Skip to contentSkip to conversation

qapi: add #if pre-processor conditions to generated code

PATCH Superseded Marc-André Lureau 7 participants 69 messages Jul 27, 2017 – Aug 29, 2017

Subsystems (5)

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

cover qapi: add #if pre-processor conditions to generated code

Hi,

In order to clean-up some hacks in qapi (having to unregister commands at runtime), I proposed a "[PATCH v5 02/20] qapi.py: add a simple #ifdef condition"

(see http://lists.gnu.org/archive/html/qemu-devel/2016-08/msg03106.html).

However, we decided to drop that patch from the series and solve the problem later. The main issues were:
- the syntax was awkward to the JSON schema and documentation
- the evaluation of the condition was done in the qapi scripts, with very limited capability
- each target/config would need different generated files.

Instead, it could defer the #if evaluation to the C-preprocessor.

With this series, top-level qapi JSON entity can take 'if' keys:

{ 'struct': 'TestIfStruct', 'data': { 'foo': 'int' },
'if': 'defined(TEST_IF_STRUCT)' }

Members can be exploded as dictionnary with 'type'/'if' keys:

{ 'struct': 'TestIfStruct', 'data':
{ 'foo': 'int',

    'bar': { 'type': 'int', 'if': 'defined(TEST_IF_STRUCT_BAR)'} } }

Enum values can be exploded as dictionnary with 'type'/'if' keys:

{ 'enum': 'TestIfEnum', 'data':
[ 'foo',

    { 'name' : 'bar', 'if': 'defined(TEST_IF_ENUM_BAR)' } ] }

A good benefit from having conditional schema is that introspection will reflect more accurately the capability of the server. Another benefit is that it may help to remove some dead code when disabling a functionality.

Starting from patch "qapi: add conditions to VNC type/commands/events on the schema", the series demonstrate adding conditions, in order to remove qmp_unregister_commands_hack(). However, it feels like I cheated a little bit by using per-target NEED_CPU_H in the headers to avoid #define poison. The alternative could be to split the headers in common/target?

There are a lot more things we could make conditional in the QAPI schema, like pci/kvm/xen/numa/vde/slirp/posix/win32/vsock/lzo etc etc, however I am still evaluating the implication of such changes both externally and internally, for those interested, I can share my wip branch.

Comments welcome,

Marc-André Lureau (26):
qapi: fix type_seen key error
qobject: replace dump_qobject() by qobject_to_string()
qboject: add literal qobject type
qapi: generate a literal qobject for introspection
visitor: pass size of strings array to enum visitor
qapi2texi: minor python code simplification
qapi: add 'if' condition on top-level schema elements
qapi: add 'if' condition on enum member values
qapi: add 'if' condition on struct member
qapi: add 'if' condition on union variant
qapi: add 'if' condition on alternate variant
qapi2texi: add 'If:' section to generated documentation
qapi2texi: add 'If:' condition to enum values
qapi2texi: add 'If:' condition to struct members
qapi2texi: add condition to variants
qapi: add conditions to VNC type/commands/events on the schema
qapi: add conditions to SPICE type/commands/events on the schema
qapi: add conditions to REPLICATION type/commands on the schema
build-sys: move qapi variables in qapi.mak tests/qmp-test: add query-qmp-schema test
build-sys: make qemu qapi objects per-target
qapi: make rtc-reset-reinjection depend on TARGET_I386
qapi: make s390 commands depend on TARGET_S390X
qapi: make query-gic-capabilities depend on TARGET_ARM
qapi: make query-cpu-model-expansion depend on s390 or x86
qapi: make query-cpu-definitions depend on specific targets

 qapi-schema.json                                 |  98 ++++++----
 qapi/event.json                                  |  21 +-
 include/qapi/visitor.h                           |   3 +-
 scripts/qapi.py                                  | 232 ++++++++++++++++++-----
 scripts/qapi-commands.py                         |   4 +-
 scripts/qapi-event.py                            |   4 +-
 scripts/qapi-introspect.py                       | 116 +++++++-----
 scripts/qapi-types.py                            |  55 ++++--
 scripts/qapi-visit.py                            |  29 ++-
 scripts/qapi2texi.py                             |  49 ++---
 include/hw/qdev-core.h                           |   1 +
 include/qapi/qmp/qdict.h                         |   2 +
 include/qapi/qmp/qlist.h                         |   2 +
 include/qapi/qmp/qlit.h                          |  53 ++++++
 include/qapi/qmp/qobject.h                       |   7 +
 include/qom/object.h                             |   4 +
 include/sysemu/arch_init.h                       |  11 --
 qapi/qapi-visit-core.c                           |  23 +--
 backends/hostmem.c                               |   1 +
 block/qapi.c                                     |  90 +--------
 crypto/secret.c                                  |   1 +
 crypto/tlscreds.c                                |   1 +
 hmp.c                                            |  14 +-
 hw/core/qdev-properties.c                        |  11 +-
 migration/colo.c                                 |  14 +-
 monitor.c                                        |  70 +------
 net/filter.c                                     |   1 +
 qmp.c                                            |  72 +------
 qobject/qdict.c                                  |  30 +++
 qobject/qlist.c                                  |  23 +++
 qobject/qlit.c                                   |  53 ++++++
 qobject/qobject.c                                |  21 ++
 qom/object.c                                     |  11 +-
 stubs/arch-query-cpu-def.c                       |  10 -
 stubs/arch-query-cpu-model-baseline.c            |  12 --
 stubs/arch-query-cpu-model-comparison.c          |  12 --
 stubs/arch-query-cpu-model-expansion.c           |  12 --
 stubs/qapi-event.c                               |  74 ++++++++
 target/arm/helper.c                              |   3 +-
 target/i386/cpu.c                                |   5 +-
 target/ppc/translate_init.c                      |   3 +-
 target/s390x/cpu_models.c                        |   9 +-
 tests/check-qjson.c                              |  91 +++++----
 tests/check-qlit.c                               |  52 +++++
 tests/check-qom-proplist.c                       |   1 +
 tests/qmp-test.c                                 |  21 ++
 tests/test-qobject-input-visitor.c               |  11 +-
 Makefile                                         |  43 ++---
 Makefile.objs                                    |   9 +-
 Makefile.target                                  |   4 +
 docs/devel/qapi-code-gen.txt                     |  29 ++-
 qapi.mak                                         |  14 ++
 qobject/Makefile.objs                            |   2 +-
 stubs/Makefile.objs                              |   5 +-
 tests/Makefile.include                           |  11 +-
 tests/qapi-schema/alternate-conflict-string.json |   4 +-
 tests/qapi-schema/alternate-dict-invalid.err     |   1 +
 tests/qapi-schema/alternate-dict-invalid.exit    |   1 +
 tests/qapi-schema/alternate-dict-invalid.json    |   4 +
 tests/qapi-schema/alternate-dict-invalid.out     |   0
 tests/qapi-schema/bad-if.err                     |   1 +
 tests/qapi-schema/bad-if.exit                    |   1 +
 tests/qapi-schema/bad-if.json                    |   3 +
 tests/qapi-schema/bad-if.out                     |   0
 tests/qapi-schema/enum-dict-member-invalid.err   |   1 +
 tests/qapi-schema/enum-dict-member-invalid.exit  |   1 +
 tests/qapi-schema/enum-dict-member-invalid.json  |   2 +
 tests/qapi-schema/enum-dict-member-invalid.out   |   0
 tests/qapi-schema/enum-dict-member.err           |   1 -
 tests/qapi-schema/enum-dict-member.exit          |   2 +-
 tests/qapi-schema/enum-dict-member.json          |   3 +-
 tests/qapi-schema/enum-dict-member.out           |   4 +
 tests/qapi-schema/enum-if-invalid.err            |   1 +
 tests/qapi-schema/enum-if-invalid.exit           |   1 +
 tests/qapi-schema/enum-if-invalid.json           |   3 +
 tests/qapi-schema/enum-if-invalid.out            |   0
 tests/qapi-schema/qapi-schema-test.json          |  32 ++++
 tests/qapi-schema/qapi-schema-test.out           |  47 +++++
 tests/qapi-schema/struct-if-invalid.err          |   1 +
 tests/qapi-schema/struct-if-invalid.exit         |   1 +
 tests/qapi-schema/struct-if-invalid.json         |   3 +
 tests/qapi-schema/struct-if-invalid.out          |   0
 tests/qapi-schema/struct-member-type.err         |   0
 tests/qapi-schema/struct-member-type.exit        |   1 +
 tests/qapi-schema/struct-member-type.json        |   1 +
 tests/qapi-schema/struct-member-type.out         |   5 +
 tests/qapi-schema/test-qapi.py                   |  31 ++-
 trace/Makefile.objs                              |   2 +-
 88 files changed, 1094 insertions(+), 624 deletions(-)
 create mode 100644 include/qapi/qmp/qlit.h
 create mode 100644 qobject/qlit.c
 delete mode 100644 stubs/arch-query-cpu-def.c
 delete mode 100644 stubs/arch-query-cpu-model-baseline.c
 delete mode 100644 stubs/arch-query-cpu-model-comparison.c
 delete mode 100644 stubs/arch-query-cpu-model-expansion.c
 create mode 100644 stubs/qapi-event.c
 create mode 100644 tests/check-qlit.c
 create mode 100644 qapi.mak
 create mode 100644 tests/qapi-schema/alternate-dict-invalid.err
 create mode 100644 tests/qapi-schema/alternate-dict-invalid.exit
 create mode 100644 tests/qapi-schema/alternate-dict-invalid.json
 create mode 100644 tests/qapi-schema/alternate-dict-invalid.out
 create mode 100644 tests/qapi-schema/bad-if.err
 create mode 100644 tests/qapi-schema/bad-if.exit
 create mode 100644 tests/qapi-schema/bad-if.json
 create mode 100644 tests/qapi-schema/bad-if.out
 create mode 100644 tests/qapi-schema/enum-dict-member-invalid.err
 create mode 100644 tests/qapi-schema/enum-dict-member-invalid.exit
 create mode 100644 tests/qapi-schema/enum-dict-member-invalid.json
 create mode 100644 tests/qapi-schema/enum-dict-member-invalid.out
 create mode 100644 tests/qapi-schema/enum-if-invalid.err
 create mode 100644 tests/qapi-schema/enum-if-invalid.exit
 create mode 100644 tests/qapi-schema/enum-if-invalid.json
 create mode 100644 tests/qapi-schema/enum-if-invalid.out
 create mode 100644 tests/qapi-schema/struct-if-invalid.err
 create mode 100644 tests/qapi-schema/struct-if-invalid.exit
 create mode 100644 tests/qapi-schema/struct-if-invalid.json
 create mode 100644 tests/qapi-schema/struct-if-invalid.out
 create mode 100644 tests/qapi-schema/struct-member-type.err
 create mode 100644 tests/qapi-schema/struct-member-type.exit
 create mode 100644 tests/qapi-schema/struct-member-type.json
 create mode 100644 tests/qapi-schema/struct-member-type.out
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#1 lore raw

PATCH 1/26 qapi: fix type_seen key error

The type_seen member can be of a different type than the 'qtype' being checked, since a string create several conflicts. Lookup the real conflicting type in the conflict set, that one must be present in type_seen.

This fixes the following error, reproducible with the modified test:

Traceback (most recent call last):
File "/home/elmarco/src/qq/tests/qapi-schema/test-qapi.py", line 56, in <module>
schema = QAPISchema(sys.argv[1])
File "/home/elmarco/src/qq/scripts/qapi.py", line 1470, in __init__
self.exprs = check_exprs(parser.exprs)
File "/home/elmarco/src/qq/scripts/qapi.py", line 959, in check_exprs
check_alternate(expr, info)
File "/home/elmarco/src/qq/scripts/qapi.py", line 831, in check_alternate

    % (name, key, types_seen[qtype]))

KeyError: 'QTYPE_QSTRING'

---
 scripts/qapi.py                                  | 4 +++-
 tests/qapi-schema/alternate-conflict-string.json | 4 ++--
 2 files changed, 5 insertions(+), 3 deletions(-)
scripts/qapi.py +3 -1
index 8aa2775f12..4ecc19e944 100644
@@ -825,7 +825,9 @@ def check_alternate(expr, info):
             else:
                 conflicting.add('QTYPE_QNUM')
                 conflicting.add('QTYPE_QBOOL')
-        if conflicting & set(types_seen):
+        conflict = conflicting & set(types_seen)
+        if conflict:
+            qtype = list(conflict)[0]
             raise QAPISemError(info, "Alternate '%s' member '%s' can't "
                                "be distinguished from member '%s'"
                                % (name, key, types_seen[qtype]))
tests/qapi-schema/alternate-conflict-string.json +2 -2
index 85adbd4adc..bb2702978e 100644
@@ -1,4 +1,4 @@
 # alternate branches of 'str' type conflict with all scalar types
 { 'alternate': 'Alt',
-  'data': { 'one': 'str',
-            'two': 'int' } }
+  'data': { 'one': 'int',
+            'two': 'str' } }
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#2 lore raw

Re: [Qemu-devel] [PATCH 01/26] qapi: fix type_seen key error

Quoted 29 lines · Marc-André Lureau

The type_seen member can be of a different type than the 'qtype' being checked, since a string create several conflicts. Lookup the real conflicting type in the conflict set, that one must be present in type_seen.

This fixes the following error, reproducible with the modified test:

Traceback (most recent call last):
File "/home/elmarco/src/qq/tests/qapi-schema/test-qapi.py", line 56, in <module>
schema = QAPISchema(sys.argv[1])
File "/home/elmarco/src/qq/scripts/qapi.py", line 1470, in __init__
self.exprs = check_exprs(parser.exprs)
File "/home/elmarco/src/qq/scripts/qapi.py", line 959, in check_exprs
check_alternate(expr, info)
File "/home/elmarco/src/qq/scripts/qapi.py", line 831, in check_alternate

    % (name, key, types_seen[qtype]))

KeyError: 'QTYPE_QSTRING'

---
 scripts/qapi.py                                  | 4 +++-
 tests/qapi-schema/alternate-conflict-string.json | 4 ++--
 2 files changed, 5 insertions(+), 3 deletions(-)
scripts/qapi.py +0 -0
index 8aa2775f12..4ecc19e944 100644

scripts/qapi.py def check_alternate(expr, info): · line 833

Quoted 10 lines
scripts/qapi.py +3 -1
@@ -825,7 +825,9 @@ def check_alternate(expr, info):
             else:
                 conflicting.add('QTYPE_QNUM')
                 conflicting.add('QTYPE_QBOOL')
-        if conflicting & set(types_seen):
+        conflict = conflicting & set(types_seen)
+        if conflict:
+            qtype = list(conflict)[0]
             raise QAPISemError(info, "Alternate '%s' member '%s' can't "
                                "be distinguished from member '%s'"
                                % (name, key, types_seen[qtype]))

I see.

The fix is uninvasive, but I'm not thrilled by repurposing @qtype, and the meaning of list(dict) is less obvious than dict.keys(). What about

conflict = conflicting & set(types_seen)
if conflict:
conflicting_member_name = conflict.keys()[0] raise QAPISemError(info, "Alternate '%s' member '%s' can't "
"be distinguished from member '%s'" % (name, key, conflicting_member_name))

Alternatively, list all conflicting names (I wouldn't bother).

tests/qapi-schema/alternate-conflict-string.json line 4

Quoted 11 lines
tests/qapi-schema/alternate-conflict-string.json +2 -2
index 85adbd4adc..bb2702978e 100644
@@ -1,4 +1,4 @@
 # alternate branches of 'str' type conflict with all scalar types
 { 'alternate': 'Alt',
-  'data': { 'one': 'str',
-            'two': 'int' } }
+  'data': { 'one': 'int',
+            'two': 'str' } }

I had to think for several minutes to convince myself that this is a better test, not just a test that happens to demonstrate a particular bug. It's hot, I'm slow :)

#3 lore raw

Re: [Qemu-devel] [PATCH 01/26] qapi: fix type_seen key error

Quoted 46 lines · Markus Armbruster
Quoted 9 lines · Marc-André Lureau

The type_seen member can be of a different type than the 'qtype' being checked, since a string create several conflicts. Lookup the real conflicting type in the conflict set, that one must be present in type_seen.

This fixes the following error, reproducible with the modified test:

Traceback (most recent call last):
File "/home/elmarco/src/qq/tests/qapi-schema/test-qapi.py", line 56,

in <module>

Quoted 6 lines

schema = QAPISchema(sys.argv[1])
File "/home/elmarco/src/qq/scripts/qapi.py", line 1470, in __init__
self.exprs = check_exprs(parser.exprs)
File "/home/elmarco/src/qq/scripts/qapi.py", line 959, in check_exprs
check_alternate(expr, info)
File "/home/elmarco/src/qq/scripts/qapi.py", line 831, in

check_alternate

Quoted 14 lines
    % (name, key, types_seen[qtype]))

KeyError: 'QTYPE_QSTRING'

---
 scripts/qapi.py                                  | 4 +++-
 tests/qapi-schema/alternate-conflict-string.json | 4 ++--
 2 files changed, 5 insertions(+), 3 deletions(-)
scripts/qapi.py +0 -0
index 8aa2775f12..4ecc19e944 100644

scripts/qapi.py def check_alternate(expr, info): · line 833

Quoted 10 lines
scripts/qapi.py +3 -1
@@ -825,7 +825,9 @@ def check_alternate(expr, info):
             else:
                 conflicting.add('QTYPE_QNUM')
                 conflicting.add('QTYPE_QBOOL')
-        if conflicting & set(types_seen):
+        conflict = conflicting & set(types_seen)
+        if conflict:
+            qtype = list(conflict)[0]
             raise QAPISemError(info, "Alternate '%s' member '%s' can't "
                                "be distinguished from member '%s'"
                                % (name, key, types_seen[qtype]))

I see.

Quoted 3 lines

The fix is uninvasive, but I'm not thrilled by repurposing @qtype, and the meaning of list(dict) is less obvious than dict.keys(). What about

it's list(set) here

Quoted 5 lines

conflict = conflicting & set(types_seen) if conflict:
conflicting_member_name = conflict.keys()[0]

so that doesn't work

Quoted 6 lines

raise QAPISemError(info, "Alternate '%s' member '%s' can't "
"be distinguished from member '%s'" % (name, key, conflicting_member_name))

Alternatively, list all conflicting names (I wouldn't bother).

That would be less accurate than today, and it's just a single qtype assignment away. Let's call it conflict_qtype if you prefer.

Quoted 18 lines

Review

Quoted 2 lines
(quoted diff) +0 -0

b/tests/qapi-schema/alternate-conflict-string.json

tests/qapi-schema/alternate-conflict-string.json line 4

Quoted 10 lines

index 85adbd4adc..bb2702978e 100644

tests/qapi-schema/alternate-conflict-string.json +2 -2
@@ -1,4 +1,4 @@
 # alternate branches of 'str' type conflict with all scalar types
 { 'alternate': 'Alt',
-  'data': { 'one': 'str',
-            'two': 'int' } }
+  'data': { 'one': 'int',
+            'two': 'str' } }

I had to think for several minutes to convince myself that this is a better test, not just a test that happens to demonstrate a particular bug. It's hot, I'm slow :)

Quoted 1 line

--

Marc-André Lureau

#4 lore raw

PATCH 2/26 qobject: replace dump_qobject() by qobject_to_string()

In reply to Marc-André Lureau

The dump functions is generally useful for any qobject user, for testing, debugging etc.

The callback-based output is replaced by string allocation. Trading efficiency for ease-of-use is okay here.

---
 include/qapi/qmp/qdict.h   |  2 ++
 include/qapi/qmp/qlist.h   |  2 ++
 include/qapi/qmp/qobject.h |  7 ++++
 block/qapi.c               | 90 +++-------------------------------------------
 qobject/qdict.c            | 30 ++++++++++++++++
 qobject/qlist.c            | 23 ++++++++++++
 qobject/qobject.c          | 21 +++++++++++
 tests/check-qjson.c        | 19 ++++++++++
 8 files changed, 108 insertions(+), 86 deletions(-)
include/qapi/qmp/qdict.h +2 -0
index 363e431106..c9c4038435 100644
@@ -86,4 +86,6 @@ QObject *qdict_crumple(const QDict *src, Error **errp);
 
 void qdict_join(QDict *dest, QDict *src, bool overwrite);
 
+char *qdict_to_string(QDict *dict, int indent);
+
 #endif /* QDICT_H */
include/qapi/qmp/qlist.h +2 -0
index c4b5fdad9b..c93ac3e15b 100644
@@ -60,6 +60,8 @@ size_t qlist_size(const QList *qlist);
 QList *qobject_to_qlist(const QObject *obj);
 void qlist_destroy_obj(QObject *obj);
 
+char *qlist_to_string(QList *list, int indent);
+
 static inline const QListEntry *qlist_first(const QList *qlist)
 {
     return QTAILQ_FIRST(&qlist->head);
include/qapi/qmp/qobject.h +7 -0
index eab29edd12..3365eb73c9 100644
@@ -105,4 +105,11 @@ static inline QNull *qnull(void)
     return &qnull_;
 }
 
+char *qobject_to_string_indent(QObject *obj, int indent);
+
+static inline char *qobject_to_string(QObject *obj)
+{
+    return qobject_to_string_indent(obj, 0);
+}
+
 #endif /* QOBJECT_H */
block/qapi.c +4 -86

105 lines not on this page · raw message

qobject/qdict.c +30 -0
index 576018e531..1cb5cab5e0 100644
@@ -1022,3 +1022,33 @@ void qdict_join(QDict *dest, QDict *src, bool overwrite)
         entry = next;
     }
 }
+
+char *qdict_to_string(QDict *dict, int indent)
+{
+    const QDictEntry *entry;
+    GString *str = g_string_new(NULL);
+
+    for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
+        QType type = qobject_type(entry->value);
+        bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
+        char *key = g_malloc(strlen(entry->key) + 1);
+        char *val = qobject_to_string_indent(entry->value, indent + 1);
+        int i;
+
+        /* replace dashes with spaces in key (variable) names */
+        for (i = 0; entry->key[i]; i++) {
+            key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
+        }
+        key[i] = 0;
+        g_string_append_printf(str, "%*s%s:", indent * 4, "", key);
+        g_string_append_c(str, composite ? '\n' : ' ');
+        g_string_append(str, val);
+        if (!composite) {
+            g_string_append_c(str, '\n');
+        }
+        g_free(val);
+        g_free(key);
+    }
+
+    return g_string_free(str, false);
+}
qobject/qlist.c +23 -0
index 86b60cb88c..b769248290 100644
@@ -158,3 +158,26 @@ void qlist_destroy_obj(QObject *obj)
 
     g_free(qlist);
 }
+
+char *qlist_to_string(QList *list, int indent)
+{
+    GString *str = g_string_new(NULL);
+    const QListEntry *entry;
+    int i = 0;
+
+    for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
+        QType type = qobject_type(entry->value);
+        bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
+        char *val = qobject_to_string_indent(entry->value, indent + 1);
+
+        g_string_append_printf(str, "%*s[%i]:", indent * 4, "", i);
+        g_string_append_c(str, composite ? '\n' : ' ');
+        g_string_append(str, val);
+        if (!composite) {
+            g_string_append_c(str, '\n');
+        }
+        g_free(val);
+    }
+
+    return g_string_free(str, false);
+}
qobject/qobject.c +21 -0
index b0cafb66f1..b70bef9ccb 100644
@@ -27,3 +27,24 @@ void qobject_destroy(QObject *obj)
     assert(QTYPE_QNULL < obj->type && obj->type < QTYPE__MAX);
     qdestroy[obj->type](obj);
 }
+
+char *qobject_to_string_indent(QObject *obj, int indent)
+{
+    switch (qobject_type(obj)) {
+    case QTYPE_QNULL:
+        return g_strdup("null");
+    case QTYPE_QNUM:
+        return qnum_to_string(qobject_to_qnum(obj));
+    case QTYPE_QSTRING:
+        return g_strdup(qstring_get_str(qobject_to_qstring(obj)));
+    case QTYPE_QDICT:
+        return qdict_to_string(qobject_to_qdict(obj), indent);
+    case QTYPE_QLIST:
+        return qlist_to_string(qobject_to_qlist(obj), indent);
+    case QTYPE_QBOOL:
+        return g_strdup(qbool_get_bool(qobject_to_qbool(obj)) ?
+                        "true" : "false");
+    default:
+        abort();
+    }
+}
tests/check-qjson.c +19 -0
index a3a97b0d99..9c42a46b7d 100644
@@ -1372,6 +1372,23 @@ static void simple_whitespace(void)
     }
 }
 
+static void qobject_to_string_test(void)
+{
+    QObject *obj;
+    char *tmp;
+
+    obj = qobject_from_json("[ 43, { 'c': { 'd' : 12 } }, [ 1, 2 ], 42 ]",
+                            &error_abort);
+    tmp = qobject_to_string(obj);
+    g_assert_cmpstr(tmp, ==,
+                    "[0]: 43\n"
+                    "[1]:\n    c:\n        d: 12\n"
+                    "[2]:\n    [0]: 1\n    [1]: 2\n"
+                    "[3]: 42\n");
+    g_free(tmp);
+    qobject_decref(obj);
+}
+
 static void simple_varargs(void)
 {
     QObject *embedded_obj;
@@ -1545,5 +1562,7 @@ int main(int argc, char **argv)
     g_test_add_func("/errors/unterminated/literal", unterminated_literal);
     g_test_add_func("/errors/limits/nesting", limits_nesting);
 
+    g_test_add_func("/qobject/to_string", qobject_to_string_test);
+
     return g_test_run();
 }
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#5 lore raw

Re: [Qemu-devel] [PATCH 02/26] qobject: replace dump_qobject() by qobject_to_string()

Quoted 7 lines · Marc-André Lureau

The dump functions is generally useful for any qobject user, for testing, debugging etc.

The callback-based output is replaced by string allocation. Trading efficiency for ease-of-use is okay here.

As far as I can tell, this patch is in this series only for the next patch's check-qlit.c. I don't like its use there, so I'm ignoring this patch for now.

#6 lore raw

PATCH 3/26 qboject: add literal qobject type

In reply to Marc-André Lureau

Promote LiteralQObject from tests/check-qjson.c to qobject/qlit.c, allowing to statically declare complex qobjects.

Add a helper qobject_from_qlit() to instantiate a literal qobject to a real QObject. Add some simple test.

---
 include/qapi/qmp/qlit.h | 53 ++++++++++++++++++++++++++++++++++++
 qobject/qlit.c          | 53 ++++++++++++++++++++++++++++++++++++
 tests/check-qjson.c     | 72 +++++++++++++++++--------------------------------
 tests/check-qlit.c      | 52 +++++++++++++++++++++++++++++++++++
 qobject/Makefile.objs   |  2 +-
 tests/Makefile.include  |  5 +++-
 6 files changed, 187 insertions(+), 50 deletions(-)
 create mode 100644 include/qapi/qmp/qlit.h
 create mode 100644 qobject/qlit.c
 create mode 100644 tests/check-qlit.c
include/qapi/qmp/qlit.h new file +53 -0
new file mode 100644
index 0000000000..fd6bfc3e69
@@ -0,0 +1,53 @@
+/*
+ * Copyright IBM, Corp. 2009
+ * Copyright (c) 2013, 2015 Red Hat Inc.
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *  Markus Armbruster <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+#ifndef QLIT_H_
+#define QLIT_H_
+
+#include "qapi-types.h"
+#include "qobject.h"
+
+typedef struct QLitDictEntry QLitDictEntry;
+typedef struct QLitObject QLitObject;
+
+struct QLitObject {
+    int type;
+    union {
+        bool qbool;
+        int64_t qnum;
+        const char *qstr;
+        QLitDictEntry *qdict;
+        QLitObject *qlist;
+    } value;
+};
+
+struct QLitDictEntry {
+    const char *key;
+    QLitObject value;
+};
+
+#define QLIT_QNULL \
+    { .type = QTYPE_QNULL }
+#define QLIT_QBOOL(val) \
+    { .type = QTYPE_QBOOL, .value.qbool = (val) }
+#define QLIT_QNUM(val) \
+    { .type = QTYPE_QNUM, .value.qnum = (val) }
+#define QLIT_QSTR(val) \
+    { .type = QTYPE_QSTRING, .value.qstr = (val) }
+#define QLIT_QDICT(val) \
+    { .type = QTYPE_QDICT, .value.qdict = (val) }
+#define QLIT_QLIST(val) \
+    { .type = QTYPE_QLIST, .value.qlist = (val) }
+
+QObject *qobject_from_qlit(const QLitObject *qlit);
+
+#endif /* QLIT_H_ */
qobject/qlit.c new file +53 -0

53 lines not on this page · raw message

tests/check-qjson.c +24 -48

177 lines not on this page · raw message

tests/check-qlit.c new file +52 -0

52 lines not on this page · raw message

qobject/Makefile.objs +1 -1
index fc8885c9a4..002d25873a 100644
@@ -1,2 +1,2 @@
-util-obj-y = qnull.o qnum.o qstring.o qdict.o qlist.o qbool.o
+util-obj-y = qnull.o qnum.o qstring.o qdict.o qlist.o qbool.o qlit.o
 util-obj-y += qjson.o qobject.o json-lexer.o json-streamer.o json-parser.o
tests/Makefile.include +4 -1
index 7af278db55..960ab8c6dd 100644
@@ -12,6 +12,8 @@ check-unit-y += tests/test-char$(EXESUF)
 gcov-files-check-qdict-y = chardev/char.c
 check-unit-y += tests/check-qnum$(EXESUF)
 gcov-files-check-qnum-y = qobject/qnum.c
+check-unit-y += tests/check-qlit$(EXESUF)
+gcov-files-check-qlit-y = qobject/qlit.c
 check-unit-y += tests/check-qstring$(EXESUF)
 gcov-files-check-qstring-y = qobject/qstring.c
 check-unit-y += tests/check-qlist$(EXESUF)
@@ -523,7 +525,7 @@ test-obj-y = tests/check-qnum.o tests/check-qstring.o tests/check-qdict.o \
 	tests/rcutorture.o tests/test-rcu-list.o \
 	tests/test-qdist.o tests/test-shift128.o \
 	tests/test-qht.o tests/qht-bench.o tests/test-qht-par.o \
-	tests/atomic_add-bench.o
+	tests/atomic_add-bench.o tests/check-qlit.o
 
 $(test-obj-y): QEMU_INCLUDES += -Itests
 QEMU_CFLAGS += -I$(SRC_PATH)/tests
@@ -541,6 +543,7 @@ test-io-obj-y = $(io-obj-y) $(test-crypto-obj-y)
 test-block-obj-y = $(block-obj-y) $(test-io-obj-y) tests/iothread.o
 
 tests/check-qnum$(EXESUF): tests/check-qnum.o $(test-util-obj-y)
+tests/check-qlit$(EXESUF): tests/check-qlit.o $(test-util-obj-y)
 tests/check-qstring$(EXESUF): tests/check-qstring.o $(test-util-obj-y)
 tests/check-qdict$(EXESUF): tests/check-qdict.o $(test-util-obj-y)
 tests/check-qlist$(EXESUF): tests/check-qlist.o $(test-util-obj-y)
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#7 lore raw

Re: [Qemu-devel] [PATCH 03/26] qboject: add literal qobject type

In the subject: s/qboject: add/qobject: Add/

Quoted 5 lines · Marc-André Lureau

Promote LiteralQObject from tests/check-qjson.c to qobject/qlit.c, allowing to statically declare complex qobjects.

Add a helper qobject_from_qlit() to instantiate a literal qobject to a real QObject. Add some simple test.

Suggest

* to also move the existing function making use of LiteralQObject compare_litqobj_to_qobj(), so other tests can use it to verify a QObject matches expectations, and

* to split the patch into the move and the addition of qobject_from_qlit().

Quoted 36 lines
---
 include/qapi/qmp/qlit.h | 53 ++++++++++++++++++++++++++++++++++++
 qobject/qlit.c          | 53 ++++++++++++++++++++++++++++++++++++
 tests/check-qjson.c     | 72 +++++++++++++++++--------------------------------
 tests/check-qlit.c      | 52 +++++++++++++++++++++++++++++++++++
 qobject/Makefile.objs   |  2 +-
 tests/Makefile.include  |  5 +++-
 6 files changed, 187 insertions(+), 50 deletions(-)
 create mode 100644 include/qapi/qmp/qlit.h
 create mode 100644 qobject/qlit.c
 create mode 100644 tests/check-qlit.c
include/qapi/qmp/qlit.h new file +16 -0
new file mode 100644
index 0000000000..fd6bfc3e69
@@ -0,0 +1,53 @@
+/*
+ * Copyright IBM, Corp. 2009
+ * Copyright (c) 2013, 2015 Red Hat Inc.
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *  Markus Armbruster <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+#ifndef QLIT_H_
+#define QLIT_H_
+
+#include "qapi-types.h"

include/qapi/qmp/qlit.h line 26

Quoted 10 lines
include/qapi/qmp/qlit.h new file +10 -0
+#include "qobject.h"
+
+typedef struct QLitDictEntry QLitDictEntry;
+typedef struct QLitObject QLitObject;
+
+struct QLitObject {
+    int type;
+    union {
+        bool qbool;
+        int64_t qnum;

Not this patch's fault: QLitObject restricts numbers to signed integers.

Quoted 312 lines
include/qapi/qmp/qlit.h new file +27 -0
+        const char *qstr;
+        QLitDictEntry *qdict;
+        QLitObject *qlist;
+    } value;
+};
+
+struct QLitDictEntry {
+    const char *key;
+    QLitObject value;
+};
+
+#define QLIT_QNULL \
+    { .type = QTYPE_QNULL }
+#define QLIT_QBOOL(val) \
+    { .type = QTYPE_QBOOL, .value.qbool = (val) }
+#define QLIT_QNUM(val) \
+    { .type = QTYPE_QNUM, .value.qnum = (val) }
+#define QLIT_QSTR(val) \
+    { .type = QTYPE_QSTRING, .value.qstr = (val) }
+#define QLIT_QDICT(val) \
+    { .type = QTYPE_QDICT, .value.qdict = (val) }
+#define QLIT_QLIST(val) \
+    { .type = QTYPE_QLIST, .value.qlist = (val) }
+
+QObject *qobject_from_qlit(const QLitObject *qlit);
+
+#endif /* QLIT_H_ */
qobject/qlit.c new file +53 -0
new file mode 100644
index 0000000000..d7407b4b34
@@ -0,0 +1,53 @@
+/*
+ * QLit literal qobject
+ *
+ * Copyright (C) 2017 Red Hat Inc.
+ *
+ * Authors:
+ *  Marc-André Lureau <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+
+#include "qapi/qmp/qlit.h"
+#include "qapi/qmp/types.h"
+
+QObject *qobject_from_qlit(const QLitObject *qlit)
+{
+    int i;
+
+    switch (qlit->type) {
+    case QTYPE_QNULL:
+        return QOBJECT(qnull());
+    case QTYPE_QNUM:
+        return QOBJECT(qnum_from_int(qlit->value.qnum));
+    case QTYPE_QSTRING:
+        return QOBJECT(qstring_from_str(qlit->value.qstr));
+    case QTYPE_QDICT: {
+        QDict *qdict = qdict_new();
+        for (i = 0; qlit->value.qdict[i].value.type != QTYPE_NONE; i++) {
+            QLitDictEntry *e = &qlit->value.qdict[i];
+
+            qdict_put_obj(qdict, e->key, qobject_from_qlit(&e->value));
+        }
+        return QOBJECT(qdict);
+    }
+    case QTYPE_QLIST: {
+        QList *qlist = qlist_new();
+
+        for (i = 0; qlit->value.qlist[i].type != QTYPE_NONE; i++) {
+            qlist_append_obj(qlist, qobject_from_qlit(&qlit->value.qlist[i]));
+        }
+        return QOBJECT(qlist);
+    }
+    case QTYPE_QBOOL:
+        return QOBJECT(qbool_from_bool(qlit->value.qbool));
+    case QTYPE_NONE:
+        assert(0);
+    }
+
+    return NULL;
+}
tests/check-qjson.c +24 -48
index 9c42a46b7d..1a95aff2ba 100644
@@ -16,6 +16,7 @@
 #include "qapi/error.h"
 #include "qapi/qmp/types.h"
 #include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qlit.h"
 #include "qemu-common.h"
 
 static void escaped_string(void)
@@ -1059,39 +1060,14 @@ static void keyword_literal(void)
     QDECREF(null);
 }
 
-typedef struct LiteralQDictEntry LiteralQDictEntry;
-typedef struct LiteralQObject LiteralQObject;
-
-struct LiteralQObject
-{
-    int type;
-    union {
-        int64_t qnum;
-        const char *qstr;
-        LiteralQDictEntry *qdict;
-        LiteralQObject *qlist;
-    } value;
-};
-
-struct LiteralQDictEntry
-{
-    const char *key;
-    LiteralQObject value;
-};
-
-#define QLIT_QNUM(val) (LiteralQObject){.type = QTYPE_QNUM, .value.qnum = (val)}
-#define QLIT_QSTR(val) (LiteralQObject){.type = QTYPE_QSTRING, .value.qstr = (val)}
-#define QLIT_QDICT(val) (LiteralQObject){.type = QTYPE_QDICT, .value.qdict = (val)}
-#define QLIT_QLIST(val) (LiteralQObject){.type = QTYPE_QLIST, .value.qlist = (val)}
-
 typedef struct QListCompareHelper
 {
     int index;
-    LiteralQObject *objs;
+    QLitObject *objs;
     int result;
 } QListCompareHelper;
 
-static int compare_litqobj_to_qobj(LiteralQObject *lhs, QObject *rhs);
+static int compare_litqobj_to_qobj(QLitObject *lhs, QObject *rhs);
 
 static void compare_helper(QObject *obj, void *opaque)
 {
@@ -1109,7 +1085,7 @@ static void compare_helper(QObject *obj, void *opaque)
     helper->result = compare_litqobj_to_qobj(&helper->objs[helper->index++], obj);
 }
 
-static int compare_litqobj_to_qobj(LiteralQObject *lhs, QObject *rhs)
+static int compare_litqobj_to_qobj(QLitObject *lhs, QObject *rhs)
 {
     int64_t val;
 
@@ -1159,23 +1135,23 @@ static void simple_dict(void)
     int i;
     struct {
         const char *encoded;
-        LiteralQObject decoded;
+        QLitObject decoded;
     } test_cases[] = {
         {
             .encoded = "{\"foo\": 42, \"bar\": \"hello world\"}",
-            .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
+            .decoded = QLIT_QDICT(((QLitDictEntry[]){
                         { "foo", QLIT_QNUM(42) },
                         { "bar", QLIT_QSTR("hello world") },
                         { }
                     })),
         }, {
             .encoded = "{}",
-            .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
+            .decoded = QLIT_QDICT(((QLitDictEntry[]){
                         { }
                     })),
         }, {
             .encoded = "{\"foo\": 43}",
-            .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
+            .decoded = QLIT_QDICT(((QLitDictEntry[]){
                         { "foo", QLIT_QNUM(43) },
                         { }
                     })),
@@ -1257,11 +1233,11 @@ static void simple_list(void)
     int i;
     struct {
         const char *encoded;
-        LiteralQObject decoded;
+        QLitObject decoded;
     } test_cases[] = {
         {
             .encoded = "[43,42]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
                         QLIT_QNUM(43),
                         QLIT_QNUM(42),
                         { }
@@ -1269,21 +1245,21 @@ static void simple_list(void)
         },
         {
             .encoded = "[43]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
                         QLIT_QNUM(43),
                         { }
                     })),
         },
         {
             .encoded = "[]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
                         { }
                     })),
         },
         {
             .encoded = "[{}]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
-                        QLIT_QDICT(((LiteralQDictEntry[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
+                        QLIT_QDICT(((QLitDictEntry[]){
                                     {},
                                         })),
                         {},
@@ -1314,11 +1290,11 @@ static void simple_whitespace(void)
     int i;
     struct {
         const char *encoded;
-        LiteralQObject decoded;
+        QLitObject decoded;
     } test_cases[] = {
         {
             .encoded = " [ 43 , 42 ]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
                         QLIT_QNUM(43),
                         QLIT_QNUM(42),
                         { }
@@ -1326,12 +1302,12 @@ static void simple_whitespace(void)
         },
         {
             .encoded = " [ 43 , { 'h' : 'b' }, [ ], 42 ]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
                         QLIT_QNUM(43),
-                        QLIT_QDICT(((LiteralQDictEntry[]){
+                        QLIT_QDICT(((QLitDictEntry[]){
                                     { "h", QLIT_QSTR("b") },
                                     { }})),
-                        QLIT_QLIST(((LiteralQObject[]){
+                        QLIT_QLIST(((QLitObject[]){
                                     { }})),
                         QLIT_QNUM(42),
                         { }
@@ -1339,13 +1315,13 @@ static void simple_whitespace(void)
         },
         {
             .encoded = " [ 43 , { 'h' : 'b' , 'a' : 32 }, [ ], 42 ]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
                         QLIT_QNUM(43),
-                        QLIT_QDICT(((LiteralQDictEntry[]){
+                        QLIT_QDICT(((QLitDictEntry[]){
                                     { "h", QLIT_QSTR("b") },
                                     { "a", QLIT_QNUM(32) },
                                     { }})),
-                        QLIT_QLIST(((LiteralQObject[]){
+                        QLIT_QLIST(((QLitObject[]){
                                     { }})),
                         QLIT_QNUM(42),
                         { }
@@ -1393,10 +1369,10 @@ static void simple_varargs(void)
 {
     QObject *embedded_obj;
     QObject *obj;
-    LiteralQObject decoded = QLIT_QLIST(((LiteralQObject[]){
+    QLitObject decoded = QLIT_QLIST(((QLitObject[]){
             QLIT_QNUM(1),
             QLIT_QNUM(2),
-            QLIT_QLIST(((LiteralQObject[]){
+            QLIT_QLIST(((QLitObject[]){
                         QLIT_QNUM(32),
                         QLIT_QNUM(42),
                         {}})),
tests/check-qlit.c new file +29 -0
new file mode 100644
index 0000000000..cda4620f92
@@ -0,0 +1,52 @@
+/*
+ * QLit unit-tests.
+ *
+ * Copyright (C) 2017 Red Hat Inc.
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+
+#include "qapi/qmp/qlit.h"
+
+static void qobject_from_qlit_test(void)
+{
+    char *str;
+    QObject *qobj = NULL;
+    QLitObject qlit = QLIT_QDICT((
+        (QLitDictEntry[]) {
+            { "foo", QLIT_QNUM(42) },
+            { "bar", QLIT_QSTR("hello world") },
+            { "baz", QLIT_QNULL },
+            { "bee", QLIT_QLIST((
+                (QLitObject[]) {
+                    QLIT_QNUM(43),
+                    QLIT_QNUM(44),
+                    QLIT_QBOOL(true),
+                    { },
+                }))

tests/check-qlit.c line 39

Quoted 10 lines
tests/check-qlit.c new file +10 -0
+            },
+            { },
+        }));
+
+    qobj = qobject_from_qlit(&qlit);
+
+    str = qobject_to_string(qobj);
+    g_assert_cmpstr(str, ==,
+                    "bee:\n    [0]: 43\n    [1]: 44\n    [2]: true\n"   \
+                    "baz: null\nbar: hello world\nfoo: 42\n");

I don't like this. The order of QDict members in @str depends on qdict_first()/qdict_next() iteration order, which is unspecified.

Here's how we check elsewhere that a QObject matches expectations:

    static void qobject_from_qlit_test(void)
    {
        QLitObject qlit = QLIT_QDICT((
            (QLitDictEntry[]) {
                { "foo", QLIT_QNUM(42) },
                { "bar", QLIT_QSTR("hello world") },
                { "baz", QLIT_QNULL },
                { "bee", QLIT_QLIST((
                    (QLitObject[]) {
                        QLIT_QNUM(43),
                        QLIT_QNUM(44),
                        QLIT_QBOOL(true),
                        { },
                    }))
                },
                { },
            }));
        QObject *qobj = qobject_from_qlit(&qlit);
        QDict *qdict;
        QList *baz;

        qdict = qobject_to_qdict(qobj);
        g_assert_cmpint(qdict_get_int(qdict, "foo"), ==, 42);
        g_assert_cmpstr(qdict_get_str(qdict, "bar"), ==, "hello world");
        g_assert(qobject_type(qdict_get(qdict, "baz")) == QTYPE_QNULL);
        baz = qdict_get_qlist(qdict, "bee");
        g_assert_cmpint(qnum_get_int(qobject_to_qnum(qlist_pop(baz))), ==, 43);
        g_assert_cmpint(qnum_get_int(qobject_to_qnum(qlist_pop(baz))), ==, 44);
        g_assert_cmpint(qbool_get_bool(qobject_to_qbool(qlist_pop(baz))), ==, 1);

        qobject_decref(qobj);
    }

Robust, just tedious.

check-qjson.c uses a differently tedious technique: compare expected QLitObject to actual QObject with compare_litqobj_to_qobj(). I like that better, because I find the QLIT... initializers easier to read, and less error prone to write. You might prefer not to use QLit to test QLit, though.

Quoted 35 lines
tests/check-qlit.c new file +13 -0
+
+    g_free(str);
+    qobject_decref(qobj);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+
+    g_test_add_func("/qlit/qobject_from_qlit", qobject_from_qlit_test);
+
+    return g_test_run();
+}
qobject/Makefile.objs +1 -1
index fc8885c9a4..002d25873a 100644
@@ -1,2 +1,2 @@
-util-obj-y = qnull.o qnum.o qstring.o qdict.o qlist.o qbool.o
+util-obj-y = qnull.o qnum.o qstring.o qdict.o qlist.o qbool.o qlit.o
 util-obj-y += qjson.o qobject.o json-lexer.o json-streamer.o json-parser.o
tests/Makefile.include +2 -0
index 7af278db55..960ab8c6dd 100644
@@ -12,6 +12,8 @@ check-unit-y += tests/test-char$(EXESUF)
 gcov-files-check-qdict-y = chardev/char.c
 check-unit-y += tests/check-qnum$(EXESUF)
 gcov-files-check-qnum-y = qobject/qnum.c
+check-unit-y += tests/check-qlit$(EXESUF)
+gcov-files-check-qlit-y = qobject/qlit.c
 check-unit-y += tests/check-qstring$(EXESUF)
 gcov-files-check-qstring-y = qobject/qstring.c
 check-unit-y += tests/check-qlist$(EXESUF)

tests/Makefile.include test-obj-y = tests/check-qnum.o tests/check-qstring.o tests/check-qdict.o \ · line 528

Quoted 5 lines
tests/Makefile.include +1 -1
@@ -523,7 +525,7 @@ test-obj-y = tests/check-qnum.o tests/check-qstring.o tests/check-qdict.o \
 	tests/rcutorture.o tests/test-rcu-list.o \
 	tests/test-qdist.o tests/test-shift128.o \
 	tests/test-qht.o tests/qht-bench.o tests/test-qht-par.o \
-	tests/atomic_add-bench.o
+	tests/atomic_add-bench.o tests/check-qlit.o

Please add it right next to the other qobject-related tests:

test-obj-y = tests/check-qnum.o tests/check-qstring.o tests/check-qdict.o \
tests/check-qlist.o tests/check-qnull.o \
- tests/check-qjson.o \
+ tests/check-qjson.o tests/check-qlit.o \

tests/Makefile.include test-io-obj-y = $(io-obj-y) $(test-crypto-obj-y) · line 549

Quoted 11 lines
tests/Makefile.include +1 -0
 $(test-obj-y): QEMU_INCLUDES += -Itests
 QEMU_CFLAGS += -I$(SRC_PATH)/tests
@@ -541,6 +543,7 @@ test-io-obj-y = $(io-obj-y) $(test-crypto-obj-y)
 test-block-obj-y = $(block-obj-y) $(test-io-obj-y) tests/iothread.o
 
 tests/check-qnum$(EXESUF): tests/check-qnum.o $(test-util-obj-y)
+tests/check-qlit$(EXESUF): tests/check-qlit.o $(test-util-obj-y)
 tests/check-qstring$(EXESUF): tests/check-qstring.o $(test-util-obj-y)
 tests/check-qdict$(EXESUF): tests/check-qdict.o $(test-util-obj-y)
 tests/check-qlist$(EXESUF): tests/check-qlist.o $(test-util-obj-y)

Same order as in test-obj-y, please.

#8 lore raw

Re: [Qemu-devel] [PATCH 03/26] qboject: add literal qobject type

Hi

Quoted 10 lines · Markus Armbruster

In the subject: s/qboject: add/qobject: Add/

Quoted 5 lines · Marc-André Lureau

Promote LiteralQObject from tests/check-qjson.c to qobject/qlit.c, allowing to statically declare complex qobjects.

Add a helper qobject_from_qlit() to instantiate a literal qobject to a real QObject. Add some simple test.

Quoted 9 lines

Suggest

* to also move the existing function making use of LiteralQObject compare_litqobj_to_qobj(), so other tests can use it to verify a QObject matches expectations, and

* to split the patch into the move and the addition of qobject_from_qlit().

ok

Quoted 410 lines
Quoted 36 lines
---
 include/qapi/qmp/qlit.h | 53 ++++++++++++++++++++++++++++++++++++
 qobject/qlit.c          | 53 ++++++++++++++++++++++++++++++++++++
 tests/check-qjson.c     | 72 +++++++++++++++++--------------------------------
 tests/check-qlit.c      | 52 +++++++++++++++++++++++++++++++++++
 qobject/Makefile.objs   |  2 +-
 tests/Makefile.include  |  5 +++-
 6 files changed, 187 insertions(+), 50 deletions(-)
 create mode 100644 include/qapi/qmp/qlit.h
 create mode 100644 qobject/qlit.c
 create mode 100644 tests/check-qlit.c
include/qapi/qmp/qlit.h new file +16 -0
new file mode 100644
index 0000000000..fd6bfc3e69
@@ -0,0 +1,53 @@
+/*
+ * Copyright IBM, Corp. 2009
+ * Copyright (c) 2013, 2015 Red Hat Inc.
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *  Markus Armbruster <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+#ifndef QLIT_H_
+#define QLIT_H_
+
+#include "qapi-types.h"

include/qapi/qmp/qlit.h line 26

Quoted 10 lines
include/qapi/qmp/qlit.h new file +10 -0
+#include "qobject.h"
+
+typedef struct QLitDictEntry QLitDictEntry;
+typedef struct QLitObject QLitObject;
+
+struct QLitObject {
+    int type;
+    union {
+        bool qbool;
+        int64_t qnum;

Not this patch's fault: QLitObject restricts numbers to signed integers.

Quoted 312 lines
include/qapi/qmp/qlit.h new file +27 -0
+        const char *qstr;
+        QLitDictEntry *qdict;
+        QLitObject *qlist;
+    } value;
+};
+
+struct QLitDictEntry {
+    const char *key;
+    QLitObject value;
+};
+
+#define QLIT_QNULL \
+    { .type = QTYPE_QNULL }
+#define QLIT_QBOOL(val) \
+    { .type = QTYPE_QBOOL, .value.qbool = (val) }
+#define QLIT_QNUM(val) \
+    { .type = QTYPE_QNUM, .value.qnum = (val) }
+#define QLIT_QSTR(val) \
+    { .type = QTYPE_QSTRING, .value.qstr = (val) }
+#define QLIT_QDICT(val) \
+    { .type = QTYPE_QDICT, .value.qdict = (val) }
+#define QLIT_QLIST(val) \
+    { .type = QTYPE_QLIST, .value.qlist = (val) }
+
+QObject *qobject_from_qlit(const QLitObject *qlit);
+
+#endif /* QLIT_H_ */
qobject/qlit.c new file +53 -0
new file mode 100644
index 0000000000..d7407b4b34
@@ -0,0 +1,53 @@
+/*
+ * QLit literal qobject
+ *
+ * Copyright (C) 2017 Red Hat Inc.
+ *
+ * Authors:
+ *  Marc-André Lureau <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+
+#include "qapi/qmp/qlit.h"
+#include "qapi/qmp/types.h"
+
+QObject *qobject_from_qlit(const QLitObject *qlit)
+{
+    int i;
+
+    switch (qlit->type) {
+    case QTYPE_QNULL:
+        return QOBJECT(qnull());
+    case QTYPE_QNUM:
+        return QOBJECT(qnum_from_int(qlit->value.qnum));
+    case QTYPE_QSTRING:
+        return QOBJECT(qstring_from_str(qlit->value.qstr));
+    case QTYPE_QDICT: {
+        QDict *qdict = qdict_new();
+        for (i = 0; qlit->value.qdict[i].value.type != QTYPE_NONE; i++) {
+            QLitDictEntry *e = &qlit->value.qdict[i];
+
+            qdict_put_obj(qdict, e->key, qobject_from_qlit(&e->value));
+        }
+        return QOBJECT(qdict);
+    }
+    case QTYPE_QLIST: {
+        QList *qlist = qlist_new();
+
+        for (i = 0; qlit->value.qlist[i].type != QTYPE_NONE; i++) {
+            qlist_append_obj(qlist, qobject_from_qlit(&qlit->value.qlist[i]));
+        }
+        return QOBJECT(qlist);
+    }
+    case QTYPE_QBOOL:
+        return QOBJECT(qbool_from_bool(qlit->value.qbool));
+    case QTYPE_NONE:
+        assert(0);
+    }
+
+    return NULL;
+}
tests/check-qjson.c +24 -48
index 9c42a46b7d..1a95aff2ba 100644
@@ -16,6 +16,7 @@
 #include "qapi/error.h"
 #include "qapi/qmp/types.h"
 #include "qapi/qmp/qjson.h"
+#include "qapi/qmp/qlit.h"
 #include "qemu-common.h"
 
 static void escaped_string(void)
@@ -1059,39 +1060,14 @@ static void keyword_literal(void)
     QDECREF(null);
 }
 
-typedef struct LiteralQDictEntry LiteralQDictEntry;
-typedef struct LiteralQObject LiteralQObject;
-
-struct LiteralQObject
-{
-    int type;
-    union {
-        int64_t qnum;
-        const char *qstr;
-        LiteralQDictEntry *qdict;
-        LiteralQObject *qlist;
-    } value;
-};
-
-struct LiteralQDictEntry
-{
-    const char *key;
-    LiteralQObject value;
-};
-
-#define QLIT_QNUM(val) (LiteralQObject){.type = QTYPE_QNUM, .value.qnum = (val)}
-#define QLIT_QSTR(val) (LiteralQObject){.type = QTYPE_QSTRING, .value.qstr = (val)}
-#define QLIT_QDICT(val) (LiteralQObject){.type = QTYPE_QDICT, .value.qdict = (val)}
-#define QLIT_QLIST(val) (LiteralQObject){.type = QTYPE_QLIST, .value.qlist = (val)}
-
 typedef struct QListCompareHelper
 {
     int index;
-    LiteralQObject *objs;
+    QLitObject *objs;
     int result;
 } QListCompareHelper;
 
-static int compare_litqobj_to_qobj(LiteralQObject *lhs, QObject *rhs);
+static int compare_litqobj_to_qobj(QLitObject *lhs, QObject *rhs);
 
 static void compare_helper(QObject *obj, void *opaque)
 {
@@ -1109,7 +1085,7 @@ static void compare_helper(QObject *obj, void *opaque)
     helper->result = compare_litqobj_to_qobj(&helper->objs[helper->index++], obj);
 }
 
-static int compare_litqobj_to_qobj(LiteralQObject *lhs, QObject *rhs)
+static int compare_litqobj_to_qobj(QLitObject *lhs, QObject *rhs)
 {
     int64_t val;
 
@@ -1159,23 +1135,23 @@ static void simple_dict(void)
     int i;
     struct {
         const char *encoded;
-        LiteralQObject decoded;
+        QLitObject decoded;
     } test_cases[] = {
         {
             .encoded = "{\"foo\": 42, \"bar\": \"hello world\"}",
-            .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
+            .decoded = QLIT_QDICT(((QLitDictEntry[]){
                         { "foo", QLIT_QNUM(42) },
                         { "bar", QLIT_QSTR("hello world") },
                         { }
                     })),
         }, {
             .encoded = "{}",
-            .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
+            .decoded = QLIT_QDICT(((QLitDictEntry[]){
                         { }
                     })),
         }, {
             .encoded = "{\"foo\": 43}",
-            .decoded = QLIT_QDICT(((LiteralQDictEntry[]){
+            .decoded = QLIT_QDICT(((QLitDictEntry[]){
                         { "foo", QLIT_QNUM(43) },
                         { }
                     })),
@@ -1257,11 +1233,11 @@ static void simple_list(void)
     int i;
     struct {
         const char *encoded;
-        LiteralQObject decoded;
+        QLitObject decoded;
     } test_cases[] = {
         {
             .encoded = "[43,42]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
                         QLIT_QNUM(43),
                         QLIT_QNUM(42),
                         { }
@@ -1269,21 +1245,21 @@ static void simple_list(void)
         },
         {
             .encoded = "[43]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
                         QLIT_QNUM(43),
                         { }
                     })),
         },
         {
             .encoded = "[]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
                         { }
                     })),
         },
         {
             .encoded = "[{}]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
-                        QLIT_QDICT(((LiteralQDictEntry[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
+                        QLIT_QDICT(((QLitDictEntry[]){
                                     {},
                                         })),
                         {},
@@ -1314,11 +1290,11 @@ static void simple_whitespace(void)
     int i;
     struct {
         const char *encoded;
-        LiteralQObject decoded;
+        QLitObject decoded;
     } test_cases[] = {
         {
             .encoded = " [ 43 , 42 ]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
                         QLIT_QNUM(43),
                         QLIT_QNUM(42),
                         { }
@@ -1326,12 +1302,12 @@ static void simple_whitespace(void)
         },
         {
             .encoded = " [ 43 , { 'h' : 'b' }, [ ], 42 ]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
                         QLIT_QNUM(43),
-                        QLIT_QDICT(((LiteralQDictEntry[]){
+                        QLIT_QDICT(((QLitDictEntry[]){
                                     { "h", QLIT_QSTR("b") },
                                     { }})),
-                        QLIT_QLIST(((LiteralQObject[]){
+                        QLIT_QLIST(((QLitObject[]){
                                     { }})),
                         QLIT_QNUM(42),
                         { }
@@ -1339,13 +1315,13 @@ static void simple_whitespace(void)
         },
         {
             .encoded = " [ 43 , { 'h' : 'b' , 'a' : 32 }, [ ], 42 ]",
-            .decoded = QLIT_QLIST(((LiteralQObject[]){
+            .decoded = QLIT_QLIST(((QLitObject[]){
                         QLIT_QNUM(43),
-                        QLIT_QDICT(((LiteralQDictEntry[]){
+                        QLIT_QDICT(((QLitDictEntry[]){
                                     { "h", QLIT_QSTR("b") },
                                     { "a", QLIT_QNUM(32) },
                                     { }})),
-                        QLIT_QLIST(((LiteralQObject[]){
+                        QLIT_QLIST(((QLitObject[]){
                                     { }})),
                         QLIT_QNUM(42),
                         { }
@@ -1393,10 +1369,10 @@ static void simple_varargs(void)
 {
     QObject *embedded_obj;
     QObject *obj;
-    LiteralQObject decoded = QLIT_QLIST(((LiteralQObject[]){
+    QLitObject decoded = QLIT_QLIST(((QLitObject[]){
             QLIT_QNUM(1),
             QLIT_QNUM(2),
-            QLIT_QLIST(((LiteralQObject[]){
+            QLIT_QLIST(((QLitObject[]){
                         QLIT_QNUM(32),
                         QLIT_QNUM(42),
                         {}})),
tests/check-qlit.c new file +29 -0
new file mode 100644
index 0000000000..cda4620f92
@@ -0,0 +1,52 @@
+/*
+ * QLit unit-tests.
+ *
+ * Copyright (C) 2017 Red Hat Inc.
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+
+#include "qapi/qmp/qlit.h"
+
+static void qobject_from_qlit_test(void)
+{
+    char *str;
+    QObject *qobj = NULL;
+    QLitObject qlit = QLIT_QDICT((
+        (QLitDictEntry[]) {
+            { "foo", QLIT_QNUM(42) },
+            { "bar", QLIT_QSTR("hello world") },
+            { "baz", QLIT_QNULL },
+            { "bee", QLIT_QLIST((
+                (QLitObject[]) {
+                    QLIT_QNUM(43),
+                    QLIT_QNUM(44),
+                    QLIT_QBOOL(true),
+                    { },
+                }))

tests/check-qlit.c line 39

Quoted 10 lines
tests/check-qlit.c new file +10 -0
+            },
+            { },
+        }));
+
+    qobj = qobject_from_qlit(&qlit);
+
+    str = qobject_to_string(qobj);
+    g_assert_cmpstr(str, ==,
+                    "bee:\n    [0]: 43\n    [1]: 44\n    [2]: true\n"   \
+                    "baz: null\nbar: hello world\nfoo: 42\n");

I don't like this. The order of QDict members in @str depends on qdict_first()/qdict_next() iteration order, which is unspecified.

Here's how we check elsewhere that a QObject matches expectations:

    static void qobject_from_qlit_test(void)
    {
        QLitObject qlit = QLIT_QDICT((
            (QLitDictEntry[]) {
                { "foo", QLIT_QNUM(42) },
                { "bar", QLIT_QSTR("hello world") },
                { "baz", QLIT_QNULL },
                { "bee", QLIT_QLIST((
                    (QLitObject[]) {
                        QLIT_QNUM(43),
                        QLIT_QNUM(44),
                        QLIT_QBOOL(true),
                        { },
                    }))
                },
                { },
            }));
        QObject *qobj = qobject_from_qlit(&qlit);
        QDict *qdict;
        QList *baz;

        qdict = qobject_to_qdict(qobj);
        g_assert_cmpint(qdict_get_int(qdict, "foo"), ==, 42);
        g_assert_cmpstr(qdict_get_str(qdict, "bar"), ==, "hello world");
        g_assert(qobject_type(qdict_get(qdict, "baz")) == QTYPE_QNULL);
        baz = qdict_get_qlist(qdict, "bee");
        g_assert_cmpint(qnum_get_int(qobject_to_qnum(qlist_pop(baz))), ==, 43);
        g_assert_cmpint(qnum_get_int(qobject_to_qnum(qlist_pop(baz))), ==, 44);
        g_assert_cmpint(qbool_get_bool(qobject_to_qbool(qlist_pop(baz))), ==, 1);

        qobject_decref(qobj);
    }
Quoted 1 line

Robust, just tedious.

done the tedious way

Quoted 7 lines

check-qjson.c uses a differently tedious technique: compare expected QLitObject to actual QObject with compare_litqobj_to_qobj(). I like that better, because I find the QLIT... initializers easier to read, and less error prone to write. You might prefer not to use QLit to test QLit, though.

ok

Quoted 34 lines
Quoted 34 lines
tests/check-qlit.c new file +13 -0
+
+    g_free(str);
+    qobject_decref(qobj);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+
+    g_test_add_func("/qlit/qobject_from_qlit", qobject_from_qlit_test);
+
+    return g_test_run();
+}
qobject/Makefile.objs +1 -1
index fc8885c9a4..002d25873a 100644
@@ -1,2 +1,2 @@
-util-obj-y = qnull.o qnum.o qstring.o qdict.o qlist.o qbool.o
+util-obj-y = qnull.o qnum.o qstring.o qdict.o qlist.o qbool.o qlit.o
 util-obj-y += qjson.o qobject.o json-lexer.o json-streamer.o json-parser.o
tests/Makefile.include +2 -0
index 7af278db55..960ab8c6dd 100644
@@ -12,6 +12,8 @@ check-unit-y += tests/test-char$(EXESUF)
 gcov-files-check-qdict-y = chardev/char.c
 check-unit-y += tests/check-qnum$(EXESUF)
 gcov-files-check-qnum-y = qobject/qnum.c
+check-unit-y += tests/check-qlit$(EXESUF)
+gcov-files-check-qlit-y = qobject/qlit.c
 check-unit-y += tests/check-qstring$(EXESUF)
 gcov-files-check-qstring-y = qobject/qstring.c
 check-unit-y += tests/check-qlist$(EXESUF)
Quoted 9 lines

tests/Makefile.include test-obj-y = tests/check-qnum.o tests/check-qstring.o tests/check-qdict.o \ · line 528

Quoted 6 lines
tests/Makefile.include +1 -1
@@ -523,7 +525,7 @@ test-obj-y = tests/check-qnum.o tests/check-qstring.o tests/check-qdict.o \
      tests/rcutorture.o tests/test-rcu-list.o \
      tests/test-qdist.o tests/test-shift128.o \
      tests/test-qht.o tests/qht-bench.o tests/test-qht-par.o \
-     tests/atomic_add-bench.o
+     tests/atomic_add-bench.o tests/check-qlit.o

Please add it right next to the other qobject-related tests:

ok

Quoted 11 lines
   test-obj-y = tests/check-qnum.o tests/check-qstring.o tests/check-qdict.o \
        tests/check-qlist.o tests/check-qnull.o \
(quoted diff) +1 -1
-       tests/check-qjson.o \
+       tests/check-qjson.o tests/check-qlit.o \
Quoted 7 lines
tests/Makefile.include +0 -0
 $(test-obj-y): QEMU_INCLUDES += -Itests
 QEMU_CFLAGS += -I$(SRC_PATH)/tests
@@ -541,6 +543,7 @@ test-io-obj-y = $(io-obj-y) $(test-crypto-obj-y)
 test-block-obj-y = $(block-obj-y) $(test-io-obj-y) tests/iothread.o
 
Quoted 8 lines

tests/Makefile.include test-io-obj-y = $(io-obj-y) $(test-crypto-obj-y) · line 549

Quoted 5 lines
tests/Makefile.include +1 -0
 tests/check-qnum$(EXESUF): tests/check-qnum.o $(test-util-obj-y)
+tests/check-qlit$(EXESUF): tests/check-qlit.o $(test-util-obj-y)
 tests/check-qstring$(EXESUF): tests/check-qstring.o $(test-util-obj-y)
 tests/check-qdict$(EXESUF): tests/check-qdict.o $(test-util-obj-y)
 tests/check-qlist$(EXESUF): tests/check-qlist.o $(test-util-obj-y)

Same order as in test-obj-y, please.

ok

Signature · 1 line
Marc-André Lureau
#9 lore raw

PATCH 4/26 qapi: generate a literal qobject for introspection

In reply to Marc-André Lureau

Replace the generated json string with a literal qobject. The later is easier to deal with, at run time, as well as compile time: #if blocks can be more easily added than in a json string.

---
 scripts/qapi-introspect.py         | 83 +++++++++++++++++++++-----------------
 monitor.c                          |  2 +-
 tests/test-qobject-input-visitor.c | 10 +++--
 docs/devel/qapi-code-gen.txt       | 29 ++++++++-----
 4 files changed, 72 insertions(+), 52 deletions(-)
scripts/qapi-introspect.py +46 -37
index 032bcea491..fc72cdb66d 100644
@@ -12,72 +12,74 @@
 from qapi import *
 
 
-# Caveman's json.dumps() replacement (we're stuck at Python 2.4)
-# TODO try to use json.dumps() once we get unstuck
-def to_json(obj, level=0):
+def to_qlit(obj, level=0, first_indent=True):
+    def indent(level):
+        return level * 4 * ' '
+    ret = ''
+    if first_indent:
+        ret += indent(level)
     if obj is None:
-        ret = 'null'
+        ret += 'QLIT_QNULL'
     elif isinstance(obj, str):
-        ret = '"' + obj.replace('"', r'\"') + '"'
+        ret += 'QLIT_QSTR(' + '"' + obj.replace('"', r'\"') + '"' + ')'
     elif isinstance(obj, list):
-        elts = [to_json(elt, level + 1)
+        elts = [to_qlit(elt, level + 1)
                 for elt in obj]
-        ret = '[' + ', '.join(elts) + ']'
+        elts.append(indent(level + 1) + "{ }")
+        ret += 'QLIT_QLIST(((QLitObject[]) {\n'
+        ret += ',\n'.join(elts) + '\n'
+        ret += indent(level) + '}))'
     elif isinstance(obj, dict):
-        elts = ['"%s": %s' % (key.replace('"', r'\"'),
-                              to_json(obj[key], level + 1))
-                for key in sorted(obj.keys())]
-        ret = '{' + ', '.join(elts) + '}'
+        elts = [ indent(level + 1) + '{ "%s", %s }' %
+                 (key.replace('"', r'\"'), to_qlit(obj[key], level + 1, False))
+                 for key in sorted(obj.keys())]
+        elts.append(indent(level + 1) + '{ }')
+        ret += 'QLIT_QDICT(((QLitDictEntry[]) {\n'
+        ret += ',\n'.join(elts) + '\n'
+        ret += indent(level) + '}))'
     else:
         assert False                # not implemented
-    if level == 1:
-        ret = '\n' + ret
     return ret
 
 
-def to_c_string(string):
-    return '"' + string.replace('\\', r'\\').replace('"', r'\"') + '"'
-
-
 class QAPISchemaGenIntrospectVisitor(QAPISchemaVisitor):
     def __init__(self, unmask):
         self._unmask = unmask
         self.defn = None
         self.decl = None
         self._schema = None
-        self._jsons = None
+        self._qlits = None
         self._used_types = None
         self._name_map = None
 
     def visit_begin(self, schema):
         self._schema = schema
-        self._jsons = []
+        self._qlits = []
         self._used_types = []
         self._name_map = {}
 
     def visit_end(self):
         # visit the types that are actually used
-        jsons = self._jsons
-        self._jsons = []
+        qlits = self._qlits
+        self._qlits = []
         for typ in self._used_types:
             typ.visit(self)
         # generate C
         # TODO can generate awfully long lines
-        jsons.extend(self._jsons)
-        name = c_name(prefix, protect=False) + 'qmp_schema_json'
+        qlits.extend(self._qlits)
+        name = c_name(prefix, protect=False) + 'qmp_schema_qlit'
         self.decl = mcgen('''
-extern const char %(c_name)s[];
+extern const QLitObject %(c_name)s;
 ''',
                           c_name=c_name(name))
-        lines = to_json(jsons).split('\n')
-        c_string = '\n    '.join([to_c_string(line) for line in lines])
+        c_string = to_qlit(qlits)
         self.defn = mcgen('''
-const char %(c_name)s[] = %(c_string)s;
+const QLitObject %(c_name)s = %(c_string)s;
 ''',
                           c_name=c_name(name),
                           c_string=c_string)
         self._schema = None
-        self._jsons = None
+        self._qlits = None
         self._used_types = None
         self._name_map = None
 
@@ -111,12 +113,12 @@ const char %(c_name)s[] = %(c_string)s;
             return '[' + self._use_type(typ.element_type) + ']'
         return self._name(typ.name)
 
-    def _gen_json(self, name, mtype, obj):
+    def _gen_qlit(self, name, mtype, obj):
         if mtype not in ('command', 'event', 'builtin', 'array'):
             name = self._name(name)
         obj['name'] = name
         obj['meta-type'] = mtype
-        self._jsons.append(obj)
+        self._qlits.append(obj)
 
     def _gen_member(self, member):
         ret = {'name': member.name, 'type': self._use_type(member.type)}
@@ -132,24 +134,24 @@ const char %(c_name)s[] = %(c_string)s;
         return {'case': variant.name, 'type': self._use_type(variant.type)}
 
     def visit_builtin_type(self, name, info, json_type):
-        self._gen_json(name, 'builtin', {'json-type': json_type})
+        self._gen_qlit(name, 'builtin', {'json-type': json_type})
 
     def visit_enum_type(self, name, info, values, prefix):
-        self._gen_json(name, 'enum', {'values': values})
+        self._gen_qlit(name, 'enum', {'values': values})
 
     def visit_array_type(self, name, info, element_type):
         element = self._use_type(element_type)
-        self._gen_json('[' + element + ']', 'array', {'element-type': element})
+        self._gen_qlit('[' + element + ']', 'array', {'element-type': element})
 
     def visit_object_type_flat(self, name, info, members, variants):
         obj = {'members': [self._gen_member(m) for m in members]}
         if variants:
             obj.update(self._gen_variants(variants.tag_member.name,
                                           variants.variants))
-        self._gen_json(name, 'object', obj)
+        self._gen_qlit(name, 'object', obj)
 
     def visit_alternate_type(self, name, info, variants):
-        self._gen_json(name, 'alternate',
+        self._gen_qlit(name, 'alternate',
                        {'members': [{'type': self._use_type(m.type)}
                                     for m in variants.variants]})
 
@@ -157,13 +159,13 @@ const char %(c_name)s[] = %(c_string)s;
                       gen, success_response, boxed):
         arg_type = arg_type or self._schema.the_empty_object_type
         ret_type = ret_type or self._schema.the_empty_object_type
-        self._gen_json(name, 'command',
+        self._gen_qlit(name, 'command',
                        {'arg-type': self._use_type(arg_type),
                         'ret-type': self._use_type(ret_type)})
 
     def visit_event(self, name, info, arg_type, boxed):
         arg_type = arg_type or self._schema.the_empty_object_type
-        self._gen_json(name, 'event', {'arg-type': self._use_type(arg_type)})
+        self._gen_qlit(name, 'event', {'arg-type': self._use_type(arg_type)})
 
 # Debugging aid: unmask QAPI schema's type names
 # We normally mask them, because they're not QMP wire ABI
@@ -205,11 +207,18 @@ h_comment = '''
 
 fdef.write(mcgen('''
 #include "qemu/osdep.h"
+#include "qapi/qmp/qlit.h"
 #include "%(prefix)sqmp-introspect.h"
 
 ''',
                  prefix=prefix))
 
+fdecl.write(mcgen('''
+#include "qemu/osdep.h"
+#include "qapi/qmp/qlit.h"
+
+'''))
+
 schema = QAPISchema(input_file)
 gen = QAPISchemaGenIntrospectVisitor(opt_unmask)
 schema.visit(gen)
monitor.c +1 -1
index 6d040e620f..a1773d5bc7 100644
@@ -953,7 +953,7 @@ EventInfoList *qmp_query_events(Error **errp)
 static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
                                  Error **errp)
 {
-    *ret_data = qobject_from_json(qmp_schema_json, &error_abort);
+    *ret_data = qobject_from_qlit(&qmp_schema_qlit);
 }
 
 /*
tests/test-qobject-input-visitor.c +6 -4
index bcf02617dc..1969733971 100644
@@ -1247,24 +1247,26 @@ static void test_visitor_in_fail_alternate(TestInputVisitorData *data,
 }
 
 static void do_test_visitor_in_qmp_introspect(TestInputVisitorData *data,
-                                              const char *schema_json)
+                                              const QLitObject *qlit)
 {
     SchemaInfoList *schema = NULL;
+    QObject *obj = qobject_from_qlit(qlit);
     Visitor *v;
 
-    v = visitor_input_test_init_raw(data, schema_json);
+    v = qobject_input_visitor_new(obj);
 
     visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
     g_assert(schema);
 
     qapi_free_SchemaInfoList(schema);
+    qobject_decref(obj);
 }
 
 static void test_visitor_in_qmp_introspect(TestInputVisitorData *data,
                                            const void *unused)
 {
-    do_test_visitor_in_qmp_introspect(data, test_qmp_schema_json);
-    do_test_visitor_in_qmp_introspect(data, qmp_schema_json);
+    do_test_visitor_in_qmp_introspect(data, &test_qmp_schema_qlit);
+    do_test_visitor_in_qmp_introspect(data, &qmp_schema_qlit);
 }
 
 int main(int argc, char **argv)
docs/devel/qapi-code-gen.txt +19 -10
index 9903ac4c19..885c61b52f 100644
@@ -1295,18 +1295,27 @@ Example:
     #ifndef EXAMPLE_QMP_INTROSPECT_H
     #define EXAMPLE_QMP_INTROSPECT_H
 
-    extern const char example_qmp_schema_json[];
+    extern const QLitObject qmp_schema_qlit;
 
     #endif
     $ cat qapi-generated/example-qmp-introspect.c
 [Uninteresting stuff omitted...]
 
-    const char example_qmp_schema_json[] = "["
-        "{\"arg-type\": \"0\", \"meta-type\": \"event\", \"name\": \"MY_EVENT\"}, "
-        "{\"arg-type\": \"1\", \"meta-type\": \"command\", \"name\": \"my-command\", \"ret-type\": \"2\"}, "
-        "{\"members\": [], \"meta-type\": \"object\", \"name\": \"0\"}, "
-        "{\"members\": [{\"name\": \"arg1\", \"type\": \"[2]\"}], \"meta-type\": \"object\", \"name\": \"1\"}, "
-        "{\"members\": [{\"name\": \"integer\", \"type\": \"int\"}, {\"default\": null, \"name\": \"string\", \"type\": \"str\"}], \"meta-type\": \"object\", \"name\": \"2\"}, "
-        "{\"element-type\": \"2\", \"meta-type\": \"array\", \"name\": \"[2]\"}, "
-        "{\"json-type\": \"int\", \"meta-type\": \"builtin\", \"name\": \"int\"}, "
-        "{\"json-type\": \"string\", \"meta-type\": \"builtin\", \"name\": \"str\"}]";
+    const QLitObject example_qmp_schema_qlit = QLIT_QLIST(((QLitObject[]) {
+        QLIT_QDICT(((QLitDictEntry[]) {
+            { "arg-type", QLIT_QSTR("0") },
+            { "meta-type", QLIT_QSTR("event") },
+            { "name", QLIT_QSTR("Event") },
+            { }
+        })),
+        QLIT_QDICT(((QLitDictEntry[]) {
+            { "members", QLIT_QLIST(((QLitObject[]) {
+                { }
+            })) },
+            { "meta-type", QLIT_QSTR("object") },
+            { "name", QLIT_QSTR("0") },
+            { }
+        })),
+        ....
+        { }
+    }));
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#10 lore raw

Re: [Qemu-devel] [PATCH 04/26] qapi: generate a literal qobject for introspection

Quoted 2 lines · Marc-André Lureau

Replace the generated json string with a literal qobject. The later is easier to deal with, at run time, as well as compile time:

at run time as well as compile time

Quoted 2 lines
                                                           #if blocks

can be more easily added than in a json string.

Future tense, e.g. "adding #if conditionals will be easier".

Quoted 14 lines
---
 scripts/qapi-introspect.py         | 83 +++++++++++++++++++++-----------------
 monitor.c                          |  2 +-
 tests/test-qobject-input-visitor.c | 10 +++--
 docs/devel/qapi-code-gen.txt       | 29 ++++++++-----
 4 files changed, 72 insertions(+), 52 deletions(-)
scripts/qapi-introspect.py +0 -0
index 032bcea491..fc72cdb66d 100644

scripts/qapi-introspect.py line 15

Quoted 7 lines
scripts/qapi-introspect.py +1 -3
@@ -12,72 +12,74 @@
 from qapi import *
 
 
-# Caveman's json.dumps() replacement (we're stuck at Python 2.4)
-# TODO try to use json.dumps() once we get unstuck
-def to_json(obj, level=0):
+def to_qlit(obj, level=0, first_indent=True):

Suggest suppress_indent=False. I prefer defaulting to False.

scripts/qapi-introspect.py line 17

Quoted 2 lines
scripts/qapi-introspect.py +2 -0
+    def indent(level):
+        return level * 4 * ' '

Blank line before and after nested function definition, please.

scripts/qapi-introspect.py line 24

Quoted 9 lines
scripts/qapi-introspect.py +5 -2
+    ret = ''
+    if first_indent:
+        ret += indent(level)
     if obj is None:
-        ret = 'null'
+        ret += 'QLIT_QNULL'
     elif isinstance(obj, str):
-        ret = '"' + obj.replace('"', r'\"') + '"'
+        ret += 'QLIT_QSTR(' + '"' + obj.replace('"', r'\"') + '"' + ')'

Why not

           ret += 'QLIT_QSTR("' + obj.replace('"', r'\"') + '")'

Hmm, make that

           ret += 'QLIT_QSTR(' + to_c_string(obj) + ')'

because it makes the meaning more obvious, and it's also more robust: it doubles backslashes.

scripts/qapi-introspect.py line 28

Quoted 6 lines
scripts/qapi-introspect.py +2 -2
     elif isinstance(obj, list):
-        elts = [to_json(elt, level + 1)
+        elts = [to_qlit(elt, level + 1)
                 for elt in obj]
-        ret = '[' + ', '.join(elts) + ']'
+        elts.append(indent(level + 1) + "{ }")

I'd prefer "{}". More of the same below.

scripts/qapi-introspect.py line 31

Quoted 3 lines
scripts/qapi-introspect.py +3 -0
+        ret += 'QLIT_QLIST(((QLitObject[]) {\n'
+        ret += ',\n'.join(elts) + '\n'
+        ret += indent(level) + '}))'

The extra pair of parenthesis in QLIT_QLIST(( ... )) is slightly ugly. Not this patch's fault. Same for QLIT_QDICT(( ... )) below.

scripts/qapi-introspect.py line 34

Quoted 7 lines
scripts/qapi-introspect.py +2 -4
     elif isinstance(obj, dict):
-        elts = ['"%s": %s' % (key.replace('"', r'\"'),
-                              to_json(obj[key], level + 1))
-                for key in sorted(obj.keys())]
-        ret = '{' + ', '.join(elts) + '}'
+        elts = [ indent(level + 1) + '{ "%s", %s }' %
+                 (key.replace('"', r'\"'), to_qlit(obj[key], level + 1, False))
$ pep8 scripts/qapi-introspect.py
scripts/qapi-introspect.py:33:17: E201 whitespace after '['

Also, break lines at operators with the least precedence, not in the middle of sub-expressions.

However

elts = [indent(level + 1)
+ ('{ "%s", %s }'
% (to_c_string(key),
to_qlit(obj[key], level + 1, suppress_indent=True)))
for key in sorted(obj.keys())]

is still illegible. I'm afraid this is simply too complex for a list comprehension. Try rewriting as a loop.

Another option would be separating off indentation: generate the C initializer unindented, then feed it to a stupid indenter that counts parentheses (round, square and curly).

Quoted 46 lines
scripts/qapi-introspect.py +9 -12
+                 for key in sorted(obj.keys())]
+        elts.append(indent(level + 1) + '{ }')
+        ret += 'QLIT_QDICT(((QLitDictEntry[]) {\n'
+        ret += ',\n'.join(elts) + '\n'
+        ret += indent(level) + '}))'
     else:
         assert False                # not implemented
-    if level == 1:
-        ret = '\n' + ret
     return ret
 
 
-def to_c_string(string):
-    return '"' + string.replace('\\', r'\\').replace('"', r'\"') + '"'
-
-
 class QAPISchemaGenIntrospectVisitor(QAPISchemaVisitor):
     def __init__(self, unmask):
         self._unmask = unmask
         self.defn = None
         self.decl = None
         self._schema = None
-        self._jsons = None
+        self._qlits = None
         self._used_types = None
         self._name_map = None
 
     def visit_begin(self, schema):
         self._schema = schema
-        self._jsons = []
+        self._qlits = []
         self._used_types = []
         self._name_map = {}
 
     def visit_end(self):
         # visit the types that are actually used
-        jsons = self._jsons
-        self._jsons = []
+        qlits = self._qlits
+        self._qlits = []
         for typ in self._used_types:
             typ.visit(self)
         # generate C
         # TODO can generate awfully long lines
-        jsons.extend(self._jsons)
-        name = c_name(prefix, protect=False) + 'qmp_schema_json'

scripts/qapi-introspect.py line 75

Quoted 10 lines
scripts/qapi-introspect.py +4 -3
+        qlits.extend(self._qlits)
+        name = c_name(prefix, protect=False) + 'qmp_schema_qlit'
         self.decl = mcgen('''
-extern const char %(c_name)s[];
+extern const QLitObject %(c_name)s;
 ''',
                           c_name=c_name(name))
-        lines = to_json(jsons).split('\n')
-        c_string = '\n    '.join([to_c_string(line) for line in lines])
+        c_string = to_qlit(qlits)

The value is simple, and it's used just once. Let's eliminate the variable.

Quoted 129 lines
scripts/qapi-introspect.py +18 -11
         self.defn = mcgen('''
-const char %(c_name)s[] = %(c_string)s;
+const QLitObject %(c_name)s = %(c_string)s;
 ''',
                           c_name=c_name(name),
                           c_string=c_string)
         self._schema = None
-        self._jsons = None
+        self._qlits = None
         self._used_types = None
         self._name_map = None
 
@@ -111,12 +113,12 @@ const char %(c_name)s[] = %(c_string)s;
             return '[' + self._use_type(typ.element_type) + ']'
         return self._name(typ.name)
 
-    def _gen_json(self, name, mtype, obj):
+    def _gen_qlit(self, name, mtype, obj):
         if mtype not in ('command', 'event', 'builtin', 'array'):
             name = self._name(name)
         obj['name'] = name
         obj['meta-type'] = mtype
-        self._jsons.append(obj)
+        self._qlits.append(obj)
 
     def _gen_member(self, member):
         ret = {'name': member.name, 'type': self._use_type(member.type)}
@@ -132,24 +134,24 @@ const char %(c_name)s[] = %(c_string)s;
         return {'case': variant.name, 'type': self._use_type(variant.type)}
 
     def visit_builtin_type(self, name, info, json_type):
-        self._gen_json(name, 'builtin', {'json-type': json_type})
+        self._gen_qlit(name, 'builtin', {'json-type': json_type})
 
     def visit_enum_type(self, name, info, values, prefix):
-        self._gen_json(name, 'enum', {'values': values})
+        self._gen_qlit(name, 'enum', {'values': values})
 
     def visit_array_type(self, name, info, element_type):
         element = self._use_type(element_type)
-        self._gen_json('[' + element + ']', 'array', {'element-type': element})
+        self._gen_qlit('[' + element + ']', 'array', {'element-type': element})
 
     def visit_object_type_flat(self, name, info, members, variants):
         obj = {'members': [self._gen_member(m) for m in members]}
         if variants:
             obj.update(self._gen_variants(variants.tag_member.name,
                                           variants.variants))
-        self._gen_json(name, 'object', obj)
+        self._gen_qlit(name, 'object', obj)
 
     def visit_alternate_type(self, name, info, variants):
-        self._gen_json(name, 'alternate',
+        self._gen_qlit(name, 'alternate',
                        {'members': [{'type': self._use_type(m.type)}
                                     for m in variants.variants]})
 
@@ -157,13 +159,13 @@ const char %(c_name)s[] = %(c_string)s;
                       gen, success_response, boxed):
         arg_type = arg_type or self._schema.the_empty_object_type
         ret_type = ret_type or self._schema.the_empty_object_type
-        self._gen_json(name, 'command',
+        self._gen_qlit(name, 'command',
                        {'arg-type': self._use_type(arg_type),
                         'ret-type': self._use_type(ret_type)})
 
     def visit_event(self, name, info, arg_type, boxed):
         arg_type = arg_type or self._schema.the_empty_object_type
-        self._gen_json(name, 'event', {'arg-type': self._use_type(arg_type)})
+        self._gen_qlit(name, 'event', {'arg-type': self._use_type(arg_type)})
 
 # Debugging aid: unmask QAPI schema's type names
 # We normally mask them, because they're not QMP wire ABI
@@ -205,11 +207,18 @@ h_comment = '''
 
 fdef.write(mcgen('''
 #include "qemu/osdep.h"
+#include "qapi/qmp/qlit.h"
 #include "%(prefix)sqmp-introspect.h"
 
 ''',
                  prefix=prefix))
 
+fdecl.write(mcgen('''
+#include "qemu/osdep.h"
+#include "qapi/qmp/qlit.h"
+
+'''))
+
 schema = QAPISchema(input_file)
 gen = QAPISchemaGenIntrospectVisitor(opt_unmask)
 schema.visit(gen)
monitor.c +1 -1
index 6d040e620f..a1773d5bc7 100644
@@ -953,7 +953,7 @@ EventInfoList *qmp_query_events(Error **errp)
 static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
                                  Error **errp)
 {
-    *ret_data = qobject_from_json(qmp_schema_json, &error_abort);
+    *ret_data = qobject_from_qlit(&qmp_schema_qlit);
 }
 
 /*
tests/test-qobject-input-visitor.c +4 -2
index bcf02617dc..1969733971 100644
@@ -1247,24 +1247,26 @@ static void test_visitor_in_fail_alternate(TestInputVisitorData *data,
 }
 
 static void do_test_visitor_in_qmp_introspect(TestInputVisitorData *data,
-                                              const char *schema_json)
+                                              const QLitObject *qlit)
 {
     SchemaInfoList *schema = NULL;
+    QObject *obj = qobject_from_qlit(qlit);
     Visitor *v;
 
-    v = visitor_input_test_init_raw(data, schema_json);
+    v = qobject_input_visitor_new(obj);
 
     visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
     g_assert(schema);
 
     qapi_free_SchemaInfoList(schema);
+    qobject_decref(obj);
 }

tests/test-qobject-input-visitor.c static void test_visitor_in_fail_alternate(TestInputVisitorData *data, · line 1271

Quoted 10 lines
tests/test-qobject-input-visitor.c +2 -2
 
 static void test_visitor_in_qmp_introspect(TestInputVisitorData *data,
                                            const void *unused)
 {
-    do_test_visitor_in_qmp_introspect(data, test_qmp_schema_json);
-    do_test_visitor_in_qmp_introspect(data, qmp_schema_json);
+    do_test_visitor_in_qmp_introspect(data, &test_qmp_schema_qlit);
+    do_test_visitor_in_qmp_introspect(data, &qmp_schema_qlit);
 }
 

These tests are only marginally useful now. Before, they ensured that a qapi-introspect.py generating invalid JSON fails at "make check" compile time. Such bugs should now fail when we compile the generated qapi-introspect.c.

Quoted 31 lines
tests/test-qobject-input-visitor.c +0 -0
 int main(int argc, char **argv)
docs/devel/qapi-code-gen.txt +7 -10
index 9903ac4c19..885c61b52f 100644
@@ -1295,18 +1295,27 @@ Example:
     #ifndef EXAMPLE_QMP_INTROSPECT_H
     #define EXAMPLE_QMP_INTROSPECT_H
 
-    extern const char example_qmp_schema_json[];
+    extern const QLitObject qmp_schema_qlit;
 
     #endif
     $ cat qapi-generated/example-qmp-introspect.c
 [Uninteresting stuff omitted...]
 
-    const char example_qmp_schema_json[] = "["
-        "{\"arg-type\": \"0\", \"meta-type\": \"event\", \"name\": \"MY_EVENT\"}, "
-        "{\"arg-type\": \"1\", \"meta-type\": \"command\", \"name\": \"my-command\", \"ret-type\": \"2\"}, "
-        "{\"members\": [], \"meta-type\": \"object\", \"name\": \"0\"}, "
-        "{\"members\": [{\"name\": \"arg1\", \"type\": \"[2]\"}], \"meta-type\": \"object\", \"name\": \"1\"}, "
-        "{\"members\": [{\"name\": \"integer\", \"type\": \"int\"}, {\"default\": null, \"name\": \"string\", \"type\": \"str\"}], \"meta-type\": \"object\", \"name\": \"2\"}, "
-        "{\"element-type\": \"2\", \"meta-type\": \"array\", \"name\": \"[2]\"}, "
-        "{\"json-type\": \"int\", \"meta-type\": \"builtin\", \"name\": \"int\"}, "
-        "{\"json-type\": \"string\", \"meta-type\": \"builtin\", \"name\": \"str\"}]";
+    const QLitObject example_qmp_schema_qlit = QLIT_QLIST(((QLitObject[]) {
+        QLIT_QDICT(((QLitDictEntry[]) {
+            { "arg-type", QLIT_QSTR("0") },
+            { "meta-type", QLIT_QSTR("event") },
+            { "name", QLIT_QSTR("Event") },
+            { }

docs/devel/qapi-code-gen.txt Example: · line 1319

Quoted 10 lines
docs/devel/qapi-code-gen.txt +10 -0
+        })),
+        QLIT_QDICT(((QLitDictEntry[]) {
+            { "members", QLIT_QLIST(((QLitObject[]) {
+                { }
+            })) },
+            { "meta-type", QLIT_QSTR("object") },
+            { "name", QLIT_QSTR("0") },
+            { }
+        })),
+        ....

Ellipsis is three dots, not four :)

Output is no longer complete (less file boilerplate). Not an issue now, but it might become one when we make the examples testable. We can restore the deleted output then.

Quoted 2 lines
docs/devel/qapi-code-gen.txt +2 -0
+        { }
+    }));
#11 lore raw

Re: [Qemu-devel] [PATCH 04/26] qapi: generate a literal qobject for introspection

Hi

Quoted 11 lines · Markus Armbruster
Quoted 2 lines · Marc-André Lureau

Replace the generated json string with a literal qobject. The later is easier to deal with, at run time, as well as compile time:

at run time as well as compile time

Quoted 2 lines
                                                           #if blocks

can be more easily added than in a json string.

Future tense, e.g. "adding #if conditionals will be easier".

ok

Quoted 18 lines
Quoted 18 lines
---
 scripts/qapi-introspect.py         | 83 +++++++++++++++++++++-----------------
 monitor.c                          |  2 +-
 tests/test-qobject-input-visitor.c | 10 +++--
 docs/devel/qapi-code-gen.txt       | 29 ++++++++-----
 4 files changed, 72 insertions(+), 52 deletions(-)
scripts/qapi-introspect.py +0 -0
index 032bcea491..fc72cdb66d 100644
@@ -12,72 +12,74 @@
 from qapi import *
 
 
Quoted 6 lines

scripts/qapi-introspect.py line 15

Quoted 4 lines
scripts/qapi-introspect.py +1 -3
-# Caveman's json.dumps() replacement (we're stuck at Python 2.4)
-# TODO try to use json.dumps() once we get unstuck
-def to_json(obj, level=0):
+def to_qlit(obj, level=0, first_indent=True):

Suggest suppress_indent=False. I prefer defaulting to False.

It's not about suppressing whole indent, just the first.

I suggest you rewrite the code in a follow-up patch if you have an idea how to do it.

I'd like this apporach in general for the whole series, since it is long and tedious to deal with that many patches, and not always easy to understand what you want. If something works, it will be easier to get it done and improve the code as follow-up. This approach worked better when we ended qapi-doc for ex, and you followed up soon after with 50 more patches ;).

Quoted 5 lines

scripts/qapi-introspect.py line 17

Quoted 3 lines
scripts/qapi-introspect.py +2 -0
+    def indent(level):
+        return level * 4 * ' '

Blank line before and after nested function definition, please.

ok

Quoted 13 lines

scripts/qapi-introspect.py line 24

Quoted 10 lines
scripts/qapi-introspect.py +5 -2
+    ret = ''
+    if first_indent:
+        ret += indent(level)
     if obj is None:
-        ret = 'null'
+        ret += 'QLIT_QNULL'
     elif isinstance(obj, str):
-        ret = '"' + obj.replace('"', r'\"') + '"'
+        ret += 'QLIT_QSTR(' + '"' + obj.replace('"', r'\"') + '"' + ')'

Why not

Quoted 8 lines
           ret += 'QLIT_QSTR("' + obj.replace('"', r'\"') + '")'

Hmm, make that

           ret += 'QLIT_QSTR(' + to_c_string(obj) + ')'

because it makes the meaning more obvious, and it's also more robust: it doubles backslashes.

I can't find to_c_string(), I added one

Quoted 9 lines

scripts/qapi-introspect.py line 28

Quoted 7 lines
scripts/qapi-introspect.py +2 -2
     elif isinstance(obj, list):
-        elts = [to_json(elt, level + 1)
+        elts = [to_qlit(elt, level + 1)
                 for elt in obj]
-        ret = '[' + ', '.join(elts) + ']'
+        elts.append(indent(level + 1) + "{ }")

I'd prefer "{}". More of the same below.

ok

Quoted 24 lines

scripts/qapi-introspect.py line 31

Quoted 4 lines
scripts/qapi-introspect.py +3 -0
+        ret += 'QLIT_QLIST(((QLitObject[]) {\n'
+        ret += ',\n'.join(elts) + '\n'
+        ret += indent(level) + '}))'

The extra pair of parenthesis in QLIT_QLIST(( ... )) is slightly ugly. Not this patch's fault. Same for QLIT_QDICT(( ... )) below.

scripts/qapi-introspect.py line 34

Quoted 7 lines
scripts/qapi-introspect.py +2 -4
     elif isinstance(obj, dict):
-        elts = ['"%s": %s' % (key.replace('"', r'\"'),
-                              to_json(obj[key], level + 1))
-                for key in sorted(obj.keys())]
-        ret = '{' + ', '.join(elts) + '}'
+        elts = [ indent(level + 1) + '{ "%s", %s }' %
+                 (key.replace('"', r'\"'), to_qlit(obj[key], level + 1, False))
$ pep8 scripts/qapi-introspect.py
scripts/qapi-introspect.py:33:17: E201 whitespace after '['

Also, break lines at operators with the least precedence, not in the middle of sub-expressions.

However

Quoted 9 lines

elts = [indent(level + 1)
+ ('{ "%s", %s }'
% (to_c_string(key),
to_qlit(obj[key], level + 1, suppress_indent=True)))
for key in sorted(obj.keys())]

is still illegible. I'm afraid this is simply too complex for a list comprehension. Try rewriting as a loop.

ok

Quoted 3 lines

Another option would be separating off indentation: generate the C initializer unindented, then feed it to a stupid indenter that counts parentheses (round, square and curly).

hmm

Quoted 250 lines
Quoted 47 lines
scripts/qapi-introspect.py +9 -12
+                 for key in sorted(obj.keys())]
+        elts.append(indent(level + 1) + '{ }')
+        ret += 'QLIT_QDICT(((QLitDictEntry[]) {\n'
+        ret += ',\n'.join(elts) + '\n'
+        ret += indent(level) + '}))'
     else:
         assert False                # not implemented
-    if level == 1:
-        ret = '\n' + ret
     return ret
 
 
-def to_c_string(string):
-    return '"' + string.replace('\\', r'\\').replace('"', r'\"') + '"'
-
-
 class QAPISchemaGenIntrospectVisitor(QAPISchemaVisitor):
     def __init__(self, unmask):
         self._unmask = unmask
         self.defn = None
         self.decl = None
         self._schema = None
-        self._jsons = None
+        self._qlits = None
         self._used_types = None
         self._name_map = None
 
     def visit_begin(self, schema):
         self._schema = schema
-        self._jsons = []
+        self._qlits = []
         self._used_types = []
         self._name_map = {}
 
     def visit_end(self):
         # visit the types that are actually used
-        jsons = self._jsons
-        self._jsons = []
+        qlits = self._qlits
+        self._qlits = []
         for typ in self._used_types:
             typ.visit(self)
         # generate C
         # TODO can generate awfully long lines
-        jsons.extend(self._jsons)
-        name = c_name(prefix, protect=False) + 'qmp_schema_json'

scripts/qapi-introspect.py line 75

Quoted 10 lines
scripts/qapi-introspect.py +4 -3
+        qlits.extend(self._qlits)
+        name = c_name(prefix, protect=False) + 'qmp_schema_qlit'
         self.decl = mcgen('''
-extern const char %(c_name)s[];
+extern const QLitObject %(c_name)s;
 ''',
                           c_name=c_name(name))
-        lines = to_json(jsons).split('\n')
-        c_string = '\n    '.join([to_c_string(line) for line in lines])
+        c_string = to_qlit(qlits)

The value is simple, and it's used just once. Let's eliminate the variable.

Quoted 129 lines
scripts/qapi-introspect.py +18 -11
         self.defn = mcgen('''
-const char %(c_name)s[] = %(c_string)s;
+const QLitObject %(c_name)s = %(c_string)s;
 ''',
                           c_name=c_name(name),
                           c_string=c_string)
         self._schema = None
-        self._jsons = None
+        self._qlits = None
         self._used_types = None
         self._name_map = None
 
@@ -111,12 +113,12 @@ const char %(c_name)s[] = %(c_string)s;
             return '[' + self._use_type(typ.element_type) + ']'
         return self._name(typ.name)
 
-    def _gen_json(self, name, mtype, obj):
+    def _gen_qlit(self, name, mtype, obj):
         if mtype not in ('command', 'event', 'builtin', 'array'):
             name = self._name(name)
         obj['name'] = name
         obj['meta-type'] = mtype
-        self._jsons.append(obj)
+        self._qlits.append(obj)
 
     def _gen_member(self, member):
         ret = {'name': member.name, 'type': self._use_type(member.type)}
@@ -132,24 +134,24 @@ const char %(c_name)s[] = %(c_string)s;
         return {'case': variant.name, 'type': self._use_type(variant.type)}
 
     def visit_builtin_type(self, name, info, json_type):
-        self._gen_json(name, 'builtin', {'json-type': json_type})
+        self._gen_qlit(name, 'builtin', {'json-type': json_type})
 
     def visit_enum_type(self, name, info, values, prefix):
-        self._gen_json(name, 'enum', {'values': values})
+        self._gen_qlit(name, 'enum', {'values': values})
 
     def visit_array_type(self, name, info, element_type):
         element = self._use_type(element_type)
-        self._gen_json('[' + element + ']', 'array', {'element-type': element})
+        self._gen_qlit('[' + element + ']', 'array', {'element-type': element})
 
     def visit_object_type_flat(self, name, info, members, variants):
         obj = {'members': [self._gen_member(m) for m in members]}
         if variants:
             obj.update(self._gen_variants(variants.tag_member.name,
                                           variants.variants))
-        self._gen_json(name, 'object', obj)
+        self._gen_qlit(name, 'object', obj)
 
     def visit_alternate_type(self, name, info, variants):
-        self._gen_json(name, 'alternate',
+        self._gen_qlit(name, 'alternate',
                        {'members': [{'type': self._use_type(m.type)}
                                     for m in variants.variants]})
 
@@ -157,13 +159,13 @@ const char %(c_name)s[] = %(c_string)s;
                       gen, success_response, boxed):
         arg_type = arg_type or self._schema.the_empty_object_type
         ret_type = ret_type or self._schema.the_empty_object_type
-        self._gen_json(name, 'command',
+        self._gen_qlit(name, 'command',
                        {'arg-type': self._use_type(arg_type),
                         'ret-type': self._use_type(ret_type)})
 
     def visit_event(self, name, info, arg_type, boxed):
         arg_type = arg_type or self._schema.the_empty_object_type
-        self._gen_json(name, 'event', {'arg-type': self._use_type(arg_type)})
+        self._gen_qlit(name, 'event', {'arg-type': self._use_type(arg_type)})
 
 # Debugging aid: unmask QAPI schema's type names
 # We normally mask them, because they're not QMP wire ABI
@@ -205,11 +207,18 @@ h_comment = '''
 
 fdef.write(mcgen('''
 #include "qemu/osdep.h"
+#include "qapi/qmp/qlit.h"
 #include "%(prefix)sqmp-introspect.h"
 
 ''',
                  prefix=prefix))
 
+fdecl.write(mcgen('''
+#include "qemu/osdep.h"
+#include "qapi/qmp/qlit.h"
+
+'''))
+
 schema = QAPISchema(input_file)
 gen = QAPISchemaGenIntrospectVisitor(opt_unmask)
 schema.visit(gen)
monitor.c +1 -1
index 6d040e620f..a1773d5bc7 100644
@@ -953,7 +953,7 @@ EventInfoList *qmp_query_events(Error **errp)
 static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
                                  Error **errp)
 {
-    *ret_data = qobject_from_json(qmp_schema_json, &error_abort);
+    *ret_data = qobject_from_qlit(&qmp_schema_qlit);
 }
 
 /*
tests/test-qobject-input-visitor.c +4 -2
index bcf02617dc..1969733971 100644
@@ -1247,24 +1247,26 @@ static void test_visitor_in_fail_alternate(TestInputVisitorData *data,
 }
 
 static void do_test_visitor_in_qmp_introspect(TestInputVisitorData *data,
-                                              const char *schema_json)
+                                              const QLitObject *qlit)
 {
     SchemaInfoList *schema = NULL;
+    QObject *obj = qobject_from_qlit(qlit);
     Visitor *v;
 
-    v = visitor_input_test_init_raw(data, schema_json);
+    v = qobject_input_visitor_new(obj);
 
     visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
     g_assert(schema);
 
     qapi_free_SchemaInfoList(schema);
+    qobject_decref(obj);
 }

tests/test-qobject-input-visitor.c static void test_visitor_in_fail_alternate(TestInputVisitorData *data, · line 1271

Quoted 10 lines
tests/test-qobject-input-visitor.c +2 -2
 
 static void test_visitor_in_qmp_introspect(TestInputVisitorData *data,
                                            const void *unused)
 {
-    do_test_visitor_in_qmp_introspect(data, test_qmp_schema_json);
-    do_test_visitor_in_qmp_introspect(data, qmp_schema_json);
+    do_test_visitor_in_qmp_introspect(data, &test_qmp_schema_qlit);
+    do_test_visitor_in_qmp_introspect(data, &qmp_schema_qlit);
 }
 

These tests are only marginally useful now. Before, they ensured that a qapi-introspect.py generating invalid JSON fails at "make check" compile time. Such bugs should now fail when we compile the generated qapi-introspect.c.

Quoted 31 lines
tests/test-qobject-input-visitor.c +0 -0
 int main(int argc, char **argv)
docs/devel/qapi-code-gen.txt +7 -10
index 9903ac4c19..885c61b52f 100644
@@ -1295,18 +1295,27 @@ Example:
     #ifndef EXAMPLE_QMP_INTROSPECT_H
     #define EXAMPLE_QMP_INTROSPECT_H
 
-    extern const char example_qmp_schema_json[];
+    extern const QLitObject qmp_schema_qlit;
 
     #endif
     $ cat qapi-generated/example-qmp-introspect.c
 [Uninteresting stuff omitted...]
 
-    const char example_qmp_schema_json[] = "["
-        "{\"arg-type\": \"0\", \"meta-type\": \"event\", \"name\": \"MY_EVENT\"}, "
-        "{\"arg-type\": \"1\", \"meta-type\": \"command\", \"name\": \"my-command\", \"ret-type\": \"2\"}, "
-        "{\"members\": [], \"meta-type\": \"object\", \"name\": \"0\"}, "
-        "{\"members\": [{\"name\": \"arg1\", \"type\": \"[2]\"}], \"meta-type\": \"object\", \"name\": \"1\"}, "
-        "{\"members\": [{\"name\": \"integer\", \"type\": \"int\"}, {\"default\": null, \"name\": \"string\", \"type\": \"str\"}], \"meta-type\": \"object\", \"name\": \"2\"}, "
-        "{\"element-type\": \"2\", \"meta-type\": \"array\", \"name\": \"[2]\"}, "
-        "{\"json-type\": \"int\", \"meta-type\": \"builtin\", \"name\": \"int\"}, "
-        "{\"json-type\": \"string\", \"meta-type\": \"builtin\", \"name\": \"str\"}]";
+    const QLitObject example_qmp_schema_qlit = QLIT_QLIST(((QLitObject[]) {
+        QLIT_QDICT(((QLitDictEntry[]) {
+            { "arg-type", QLIT_QSTR("0") },
+            { "meta-type", QLIT_QSTR("event") },
+            { "name", QLIT_QSTR("Event") },
+            { }

docs/devel/qapi-code-gen.txt Example: · line 1319

Quoted 10 lines
docs/devel/qapi-code-gen.txt +10 -0
+        })),
+        QLIT_QDICT(((QLitDictEntry[]) {
+            { "members", QLIT_QLIST(((QLitObject[]) {
+                { }
+            })) },
+            { "meta-type", QLIT_QSTR("object") },
+            { "name", QLIT_QSTR("0") },
+            { }
+        })),
+        ....

Ellipsis is three dots, not four :)

Quoted 4 lines

Output is no longer complete (less file boilerplate). Not an issue now, but it might become one when we make the examples testable. We can restore the deleted output then.

ok

Signature · 1 line
Marc-André Lureau
#12 lore raw

Re: [Qemu-devel] [PATCH 04/26] qapi: generate a literal qobject for introspection

In reply to Marc-André Lureau
Quoted 5 lines · Marc-André Lureau

Replace the generated json string with a literal qobject. The later is easier to deal with, at run time, as well as compile time: #if blocks can be more easily added than in a json string.

[...]

Quoted 14 lines
tests/test-qobject-input-visitor.c +2 -1
index bcf02617dc..1969733971 100644
@@ -1247,24 +1247,26 @@ static void test_visitor_in_fail_alternate(TestInputVisitorData *data,
 }
 
 static void do_test_visitor_in_qmp_introspect(TestInputVisitorData *data,
-                                              const char *schema_json)
+                                              const QLitObject *qlit)
 {
     SchemaInfoList *schema = NULL;
+    QObject *obj = qobject_from_qlit(qlit);
     Visitor *v;

tests/test-qobject-input-visitor.c static void test_visitor_in_fail_alternate(TestInputVisitorData *data, · line 1263

Quoted 10 lines
tests/test-qobject-input-visitor.c +2 -1
 
-    v = visitor_input_test_init_raw(data, schema_json);
+    v = qobject_input_visitor_new(obj);
 
     visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
     g_assert(schema);
 
     qapi_free_SchemaInfoList(schema);
+    qobject_decref(obj);
 }

Are you leaking @v?

Quoted 11 lines
tests/test-qobject-input-visitor.c +2 -2
 static void test_visitor_in_qmp_introspect(TestInputVisitorData *data,
                                            const void *unused)
 {
-    do_test_visitor_in_qmp_introspect(data, test_qmp_schema_json);
-    do_test_visitor_in_qmp_introspect(data, qmp_schema_json);
+    do_test_visitor_in_qmp_introspect(data, &test_qmp_schema_qlit);
+    do_test_visitor_in_qmp_introspect(data, &qmp_schema_qlit);
 }
 
 int main(int argc, char **argv)

[...]

#13 lore raw

PATCH 5/26 visitor: pass size of strings array to enum visitor

In reply to Marc-André Lureau

The size is known at compile time, this avoids having to compute to check array boundaries.

Additionally, the following conditional enum entry change will create "hole" in the generated _lookup tables, that should be skipped.

---
 include/qapi/visitor.h             |  3 ++-
 scripts/qapi-visit.py              | 10 +++++-----
 include/hw/qdev-core.h             |  1 +
 include/qom/object.h               |  4 ++++
 qapi/qapi-visit-core.c             | 23 ++++++++++++-----------
 backends/hostmem.c                 |  1 +
 crypto/secret.c                    |  1 +
 crypto/tlscreds.c                  |  1 +
 hw/core/qdev-properties.c          | 11 +++++++++--
 net/filter.c                       |  1 +
 qom/object.c                       | 11 ++++++++---
 tests/check-qom-proplist.c         |  1 +
 tests/test-qobject-input-visitor.c |  2 +-
 13 files changed, 47 insertions(+), 23 deletions(-)
include/qapi/visitor.h +2 -1
index fe9faf469f..a2d9786c52 100644
@@ -469,7 +469,8 @@ bool visit_optional(Visitor *v, const char *name, bool *present);
  * that visit_type_str() must have no unwelcome side effects.
  */
 void visit_type_enum(Visitor *v, const char *name, int *obj,
-                     const char *const strings[], Error **errp);
+                     const char *const strings[], int nstrings,
+                     Error **errp);
 
 /*
  * Check if visitor is an input visitor.
scripts/qapi-visit.py +5 -5
index bd0b742236..60850a6cdd 100644
@@ -147,17 +147,17 @@ out:
                  c_name=c_name(name), c_elt_type=element_type.c_name())
 
 
-def gen_visit_enum(name):
+def gen_visit_enum(name, prefix):
     return mcgen('''
 
 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
 {
     int value = *obj;
-    visit_type_enum(v, name, &value, %(c_name)s_lookup, errp);
+    visit_type_enum(v, name, &value, %(c_name)s_lookup, %(c_max)s, errp);
     *obj = value;
 }
 ''',
-                 c_name=c_name(name))
+                 c_name=c_name(name), c_max=c_enum_const(name, '_MAX', prefix))
 
 
 def gen_visit_alternate(name, variants):
@@ -288,10 +288,10 @@ class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
         if not info:
             self._btin += gen_visit_decl(name, scalar=True)
             if do_builtins:
-                self.defn += gen_visit_enum(name)
+                self.defn += gen_visit_enum(name, prefix)
         else:
             self.decl += gen_visit_decl(name, scalar=True)
-            self.defn += gen_visit_enum(name)
+            self.defn += gen_visit_enum(name, prefix)
 
     def visit_array_type(self, name, info, element_type):
         decl = gen_visit_decl(name)
include/hw/qdev-core.h +1 -0
index ae317286a4..f86a0e1a75 100644
@@ -250,6 +250,7 @@ struct PropertyInfo {
     const char *name;
     const char *description;
     const char * const *enum_table;
+    int enum_table_size;
     int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
     void (*set_default_value)(Object *obj, const Property *prop);
     void (*create)(Object *obj, Property *prop, Error **errp);
include/qom/object.h +4 -0
index 1b828994fa..53d807e1e6 100644
@@ -1406,6 +1406,8 @@ void object_class_property_add_bool(ObjectClass *klass, const char *name,
  * @obj: the object to add a property to
  * @name: the name of the property
  * @typename: the name of the enum data type
+ * @strings: an array of strings for the enum
+ * @nstrings: the size of @strings
  * @get: the getter or %NULL if the property is write-only.
  * @set: the setter or %NULL if the property is read-only
  * @errp: if an error occurs, a pointer to an area to store the error
@@ -1416,6 +1418,7 @@ void object_class_property_add_bool(ObjectClass *klass, const char *name,
 void object_property_add_enum(Object *obj, const char *name,
                               const char *typename,
                               const char * const *strings,
+                              int nstrings,
                               int (*get)(Object *, Error **),
                               void (*set)(Object *, int, Error **),
                               Error **errp);
@@ -1423,6 +1426,7 @@ void object_property_add_enum(Object *obj, const char *name,
 void object_class_property_add_enum(ObjectClass *klass, const char *name,
                                     const char *typename,
                                     const char * const *strings,
+                                    int nstrings,
                                     int (*get)(Object *, Error **),
                                     void (*set)(Object *, int, Error **),
                                     Error **errp);
qapi/qapi-visit-core.c +12 -11
index ed6d2af462..dc0b9f2cee 100644
@@ -333,14 +333,13 @@ void visit_type_null(Visitor *v, const char *name, QNull **obj,
 }
 
 static void output_type_enum(Visitor *v, const char *name, int *obj,
-                             const char *const strings[], Error **errp)
+                             const char *const strings[],
+                             int nstrings, Error **errp)
 {
-    int i = 0;
     int value = *obj;
     char *enum_str;
 
-    while (strings[i++] != NULL);
-    if (value < 0 || value >= i - 1) {
+    if (value < 0 || value >= nstrings) {
         error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null");
         return;
     }
@@ -350,7 +349,8 @@ static void output_type_enum(Visitor *v, const char *name, int *obj,
 }
 
 static void input_type_enum(Visitor *v, const char *name, int *obj,
-                            const char *const strings[], Error **errp)
+                            const char *const strings[],
+                            int nstrings, Error **errp)
 {
     Error *local_err = NULL;
     int64_t value = 0;
@@ -362,14 +362,14 @@ static void input_type_enum(Visitor *v, const char *name, int *obj,
         return;
     }
 
-    while (strings[value] != NULL) {
-        if (strcmp(strings[value], enum_str) == 0) {
+    while (value < nstrings) {
+        if (strings[value] && strcmp(strings[value], enum_str) == 0) {
             break;
         }
         value++;
     }
 
-    if (strings[value] == NULL) {
+    if (value >= nstrings || strings[value] == NULL) {
         error_setg(errp, QERR_INVALID_PARAMETER, enum_str);
         g_free(enum_str);
         return;
@@ -380,16 +380,17 @@ static void input_type_enum(Visitor *v, const char *name, int *obj,
 }
 
 void visit_type_enum(Visitor *v, const char *name, int *obj,
-                     const char *const strings[], Error **errp)
+                     const char *const strings[], int nstrings,
+                     Error **errp)
 {
     assert(obj && strings);
     trace_visit_type_enum(v, name, obj);
     switch (v->type) {
     case VISITOR_INPUT:
-        input_type_enum(v, name, obj, strings, errp);
+        input_type_enum(v, name, obj, strings, nstrings, errp);
         break;
     case VISITOR_OUTPUT:
-        output_type_enum(v, name, obj, strings, errp);
+        output_type_enum(v, name, obj, strings, nstrings, errp);
         break;
     case VISITOR_CLONE:
         /* nothing further to do, scalar value was already copied by
backends/hostmem.c +1 -0
index 4606b73849..fc475a5387 100644
@@ -396,6 +396,7 @@ host_memory_backend_class_init(ObjectClass *oc, void *data)
         NULL, NULL, &error_abort);
     object_class_property_add_enum(oc, "policy", "HostMemPolicy",
         HostMemPolicy_lookup,
+        HOST_MEM_POLICY__MAX,
         host_memory_backend_get_policy,
         host_memory_backend_set_policy, &error_abort);
     object_class_property_add_str(oc, "id", get_id, set_id, &error_abort);
crypto/secret.c +1 -0
index 285ab7a63c..b5382cb7e3 100644
@@ -379,6 +379,7 @@ qcrypto_secret_class_init(ObjectClass *oc, void *data)
     object_class_property_add_enum(oc, "format",
                                    "QCryptoSecretFormat",
                                    QCryptoSecretFormat_lookup,
+                                   QCRYPTO_SECRET_FORMAT__MAX,
                                    qcrypto_secret_prop_get_format,
                                    qcrypto_secret_prop_set_format,
                                    NULL);
crypto/tlscreds.c +1 -0
index a8965531b6..8c060127ea 100644
@@ -234,6 +234,7 @@ qcrypto_tls_creds_class_init(ObjectClass *oc, void *data)
     object_class_property_add_enum(oc, "endpoint",
                                    "QCryptoTLSCredsEndpoint",
                                    QCryptoTLSCredsEndpoint_lookup,
+                                   QCRYPTO_TLS_CREDS_ENDPOINT__MAX,
                                    qcrypto_tls_creds_prop_get_endpoint,
                                    qcrypto_tls_creds_prop_set_endpoint,
                                    NULL);
hw/core/qdev-properties.c +9 -2
index 078fc5d239..696fed5b5b 100644
@@ -52,7 +52,8 @@ static void get_enum(Object *obj, Visitor *v, const char *name, void *opaque,
     Property *prop = opaque;
     int *ptr = qdev_get_prop_ptr(dev, prop);
 
-    visit_type_enum(v, prop->name, ptr, prop->info->enum_table, errp);
+    visit_type_enum(v, prop->name, ptr, prop->info->enum_table,
+                    prop->info->enum_table_size, errp);
 }
 
 static void set_enum(Object *obj, Visitor *v, const char *name, void *opaque,
@@ -67,7 +68,8 @@ static void set_enum(Object *obj, Visitor *v, const char *name, void *opaque,
         return;
     }
 
-    visit_type_enum(v, prop->name, ptr, prop->info->enum_table, errp);
+    visit_type_enum(v, prop->name, ptr, prop->info->enum_table,
+                    prop->info->enum_table_size, errp);
 }
 
 static void set_default_value_enum(Object *obj, const Property *prop)
@@ -586,6 +588,7 @@ const PropertyInfo qdev_prop_on_off_auto = {
     .name = "OnOffAuto",
     .description = "on/off/auto",
     .enum_table = OnOffAuto_lookup,
+    .enum_table_size = ON_OFF_AUTO__MAX,
     .get = get_enum,
     .set = set_enum,
     .set_default_value = set_default_value_enum,
@@ -598,6 +601,7 @@ QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int));
 const PropertyInfo qdev_prop_losttickpolicy = {
     .name  = "LostTickPolicy",
     .enum_table  = LostTickPolicy_lookup,
+    .enum_table_size = LOST_TICK_POLICY__MAX,
     .get   = get_enum,
     .set   = set_enum,
     .set_default_value = set_default_value_enum,
@@ -612,6 +616,7 @@ const PropertyInfo qdev_prop_blockdev_on_error = {
     .description = "Error handling policy, "
                    "report/ignore/enospc/stop/auto",
     .enum_table = BlockdevOnError_lookup,
+    .enum_table_size = BLOCKDEV_ON_ERROR__MAX,
     .get = get_enum,
     .set = set_enum,
     .set_default_value = set_default_value_enum,
@@ -626,6 +631,7 @@ const PropertyInfo qdev_prop_bios_chs_trans = {
     .description = "Logical CHS translation algorithm, "
                    "auto/none/lba/large/rechs",
     .enum_table = BiosAtaTranslation_lookup,
+    .enum_table_size = BIOS_ATA_TRANSLATION__MAX,
     .get = get_enum,
     .set = set_enum,
     .set_default_value = set_default_value_enum,
@@ -638,6 +644,7 @@ const PropertyInfo qdev_prop_fdc_drive_type = {
     .description = "FDC drive type, "
                    "144/288/120/none/auto",
     .enum_table = FloppyDriveType_lookup,
+    .enum_table_size = FLOPPY_DRIVE_TYPE__MAX,
     .get = get_enum,
     .set = set_enum,
     .set_default_value = set_default_value_enum,
net/filter.c +1 -0
index 1dfd2caa23..cf62851344 100644
@@ -180,6 +180,7 @@ static void netfilter_init(Object *obj)
                             NULL);
     object_property_add_enum(obj, "queue", "NetFilterDirection",
                              NetFilterDirection_lookup,
+                             NET_FILTER_DIRECTION__MAX,
                              netfilter_get_direction, netfilter_set_direction,
                              NULL);
     object_property_add_str(obj, "status",
qom/object.c +8 -3
index fe6e744b4d..425bae3a2a 100644
@@ -1247,6 +1247,7 @@ uint64_t object_property_get_uint(Object *obj, const char *name,
 
 typedef struct EnumProperty {
     const char * const *strings;
+    int nstrings;
     int (*get)(Object *, Error **);
     void (*set)(Object *, int, Error **);
 } EnumProperty;
@@ -1284,7 +1285,7 @@ int object_property_get_enum(Object *obj, const char *name,
     visit_complete(v, &str);
     visit_free(v);
     v = string_input_visitor_new(str);
-    visit_type_enum(v, name, &ret, enumprop->strings, errp);
+    visit_type_enum(v, name, &ret, enumprop->strings, enumprop->nstrings, errp);
 
     g_free(str);
     visit_free(v);
@@ -1950,7 +1951,7 @@ static void property_get_enum(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    visit_type_enum(v, name, &value, prop->strings, errp);
+    visit_type_enum(v, name, &value, prop->strings, prop->nstrings, errp);
 }
 
 static void property_set_enum(Object *obj, Visitor *v, const char *name,
@@ -1960,7 +1961,7 @@ static void property_set_enum(Object *obj, Visitor *v, const char *name,
     int value;
     Error *err = NULL;
 
-    visit_type_enum(v, name, &value, prop->strings, &err);
+    visit_type_enum(v, name, &value, prop->strings, prop->nstrings, &err);
     if (err) {
         error_propagate(errp, err);
         return;
@@ -1978,6 +1979,7 @@ static void property_release_enum(Object *obj, const char *name,
 void object_property_add_enum(Object *obj, const char *name,
                               const char *typename,
                               const char * const *strings,
+                              int nstrings,
                               int (*get)(Object *, Error **),
                               void (*set)(Object *, int, Error **),
                               Error **errp)
@@ -1986,6 +1988,7 @@ void object_property_add_enum(Object *obj, const char *name,
     EnumProperty *prop = g_malloc(sizeof(*prop));
 
     prop->strings = strings;
+    prop->nstrings = nstrings;
     prop->get = get;
     prop->set = set;
 
@@ -2003,6 +2006,7 @@ void object_property_add_enum(Object *obj, const char *name,
 void object_class_property_add_enum(ObjectClass *klass, const char *name,
                                     const char *typename,
                                     const char * const *strings,
+                                    int nstrings,
                                     int (*get)(Object *, Error **),
                                     void (*set)(Object *, int, Error **),
                                     Error **errp)
@@ -2011,6 +2015,7 @@ void object_class_property_add_enum(ObjectClass *klass, const char *name,
     EnumProperty *prop = g_malloc(sizeof(*prop));
 
     prop->strings = strings;
+    prop->nstrings = nstrings;
     prop->get = get;
     prop->set = set;
 
tests/check-qom-proplist.c +1 -0
index 432b66585f..1179030248 100644
@@ -143,6 +143,7 @@ static void dummy_class_init(ObjectClass *cls, void *data)
     object_class_property_add_enum(cls, "av",
                                    "DummyAnimal",
                                    dummy_animal_map,
+                                   DUMMY_LAST,
                                    dummy_get_av,
                                    dummy_set_av,
                                    NULL);
tests/test-qobject-input-visitor.c +1 -1
index 1969733971..4da5d02c35 100644
@@ -1110,7 +1110,7 @@ static void test_visitor_in_fail_struct_missing(TestInputVisitorData *data,
     error_free_or_abort(&err);
     visit_optional(v, "optional", &present);
     g_assert(!present);
-    visit_type_enum(v, "enum", &en, EnumOne_lookup, &err);
+    visit_type_enum(v, "enum", &en, EnumOne_lookup, ENUM_ONE__MAX, &err);
     error_free_or_abort(&err);
     visit_type_int(v, "i64", &i64, &err);
     error_free_or_abort(&err);
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#14 lore raw

Re: [Qemu-devel] [PATCH 05/26] visitor: pass size of strings array to enum visitor

Quoted 7 lines · Marc-André Lureau

The size is known at compile time, this avoids having to compute to check array boundaries.

Additionally, the following conditional enum entry change will create "hole" in the generated _lookup tables, that should be skipped.

Quoted 15 lines
---
 include/qapi/visitor.h             |  3 ++-
 scripts/qapi-visit.py              | 10 +++++-----
 include/hw/qdev-core.h             |  1 +
 include/qom/object.h               |  4 ++++
 qapi/qapi-visit-core.c             | 23 ++++++++++++-----------
 backends/hostmem.c                 |  1 +
 crypto/secret.c                    |  1 +
 crypto/tlscreds.c                  |  1 +
 hw/core/qdev-properties.c          | 11 +++++++++--
 net/filter.c                       |  1 +
 qom/object.c                       | 11 ++++++++---
 tests/check-qom-proplist.c         |  1 +
 tests/test-qobject-input-visitor.c |  2 +-
 13 files changed, 47 insertions(+), 23 deletions(-)

No change to scripts/qapi-types.c. The generated lookup tables continue to contain the NULL sentinel. Possibly intentional, because it saves you the trouble of searching for uses of FOO_lookup[FOO__MAX].

Quoted 69 lines
include/qapi/visitor.h +2 -1
index fe9faf469f..a2d9786c52 100644
@@ -469,7 +469,8 @@ bool visit_optional(Visitor *v, const char *name, bool *present);
  * that visit_type_str() must have no unwelcome side effects.
  */
 void visit_type_enum(Visitor *v, const char *name, int *obj,
-                     const char *const strings[], Error **errp);
+                     const char *const strings[], int nstrings,
+                     Error **errp);
 
 /*
  * Check if visitor is an input visitor.
scripts/qapi-visit.py +5 -5
index bd0b742236..60850a6cdd 100644
@@ -147,17 +147,17 @@ out:
                  c_name=c_name(name), c_elt_type=element_type.c_name())
 
 
-def gen_visit_enum(name):
+def gen_visit_enum(name, prefix):
     return mcgen('''
 
 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
 {
     int value = *obj;
-    visit_type_enum(v, name, &value, %(c_name)s_lookup, errp);
+    visit_type_enum(v, name, &value, %(c_name)s_lookup, %(c_max)s, errp);
     *obj = value;
 }
 ''',
-                 c_name=c_name(name))
+                 c_name=c_name(name), c_max=c_enum_const(name, '_MAX', prefix))
 
 
 def gen_visit_alternate(name, variants):
@@ -288,10 +288,10 @@ class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
         if not info:
             self._btin += gen_visit_decl(name, scalar=True)
             if do_builtins:
-                self.defn += gen_visit_enum(name)
+                self.defn += gen_visit_enum(name, prefix)
         else:
             self.decl += gen_visit_decl(name, scalar=True)
-            self.defn += gen_visit_enum(name)
+            self.defn += gen_visit_enum(name, prefix)
 
     def visit_array_type(self, name, info, element_type):
         decl = gen_visit_decl(name)
include/hw/qdev-core.h +1 -0
index ae317286a4..f86a0e1a75 100644
@@ -250,6 +250,7 @@ struct PropertyInfo {
     const char *name;
     const char *description;
     const char * const *enum_table;
+    int enum_table_size;
     int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
     void (*set_default_value)(Object *obj, const Property *prop);
     void (*create)(Object *obj, Property *prop, Error **errp);
include/qom/object.h +0 -0
index 1b828994fa..53d807e1e6 100644

include/qom/object.h void object_class_property_add_bool(ObjectClass *klass, const char *name, · line 1409

Quoted 4 lines
include/qom/object.h +1 -0
@@ -1406,6 +1406,8 @@ void object_class_property_add_bool(ObjectClass *klass, const char *name,
  * @obj: the object to add a property to
  * @name: the name of the property
  * @typename: the name of the enum data type
+ * @strings: an array of strings for the enum

Fixes a preexisting doc buglet.

Quoted 27 lines
include/qom/object.h +3 -0
+ * @nstrings: the size of @strings
  * @get: the getter or %NULL if the property is write-only.
  * @set: the setter or %NULL if the property is read-only
  * @errp: if an error occurs, a pointer to an area to store the error
@@ -1416,6 +1418,7 @@ void object_class_property_add_bool(ObjectClass *klass, const char *name,
 void object_property_add_enum(Object *obj, const char *name,
                               const char *typename,
                               const char * const *strings,
+                              int nstrings,
                               int (*get)(Object *, Error **),
                               void (*set)(Object *, int, Error **),
                               Error **errp);
@@ -1423,6 +1426,7 @@ void object_property_add_enum(Object *obj, const char *name,
 void object_class_property_add_enum(ObjectClass *klass, const char *name,
                                     const char *typename,
                                     const char * const *strings,
+                                    int nstrings,
                                     int (*get)(Object *, Error **),
                                     void (*set)(Object *, int, Error **),
                                     Error **errp);
qapi/qapi-visit-core.c +0 -0
index ed6d2af462..dc0b9f2cee 100644
@@ -333,14 +333,13 @@ void visit_type_null(Visitor *v, const char *name, QNull **obj,
 }
 

qapi/qapi-visit-core.c void visit_type_null(Visitor *v, const char *name, QNull **obj, · line 342

Quoted 10 lines
qapi/qapi-visit-core.c +2 -3
 static void output_type_enum(Visitor *v, const char *name, int *obj,
-                             const char *const strings[], Error **errp)
+                             const char *const strings[],
+                             int nstrings, Error **errp)
 {
-    int i = 0;
     int value = *obj;
     char *enum_str;
 
-    while (strings[i++] != NULL);

This is the computation we save.

Quoted 16 lines
qapi/qapi-visit-core.c +3 -2
-    if (value < 0 || value >= i - 1) {
+    if (value < 0 || value >= nstrings) {
         error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null");
         return;
     }
@@ -350,7 +349,8 @@ static void output_type_enum(Visitor *v, const char *name, int *obj,
 }
 
 static void input_type_enum(Visitor *v, const char *name, int *obj,
-                            const char *const strings[], Error **errp)
+                            const char *const strings[],
+                            int nstrings, Error **errp)
 {
     Error *local_err = NULL;
     int64_t value = 0;

qapi/qapi-visit-core.c static void input_type_enum(Visitor *v, const char *name, int *obj, · line 365

Quoted 4 lines
qapi/qapi-visit-core.c +0 -1
@@ -362,14 +362,14 @@ static void input_type_enum(Visitor *v, const char *name, int *obj,
         return;
     }
 
-    while (strings[value] != NULL) {

This is the predicate that becomes invalid when we put holes into strings[].

qapi/qapi-visit-core.c static void input_type_enum(Visitor *v, const char *name, int *obj, · line 366

Quoted 3 lines
qapi/qapi-visit-core.c +2 -1
-        if (strcmp(strings[value], enum_str) == 0) {
+    while (value < nstrings) {
+        if (strings[value] && strcmp(strings[value], enum_str) == 0) {

I'd prefer !strcmp().

qapi/qapi-visit-core.c static void input_type_enum(Visitor *v, const char *name, int *obj, · line 372

Quoted 7 lines
qapi/qapi-visit-core.c +1 -1
             break;
         }
         value++;
     }
 
-    if (strings[value] == NULL) {
+    if (value >= nstrings || strings[value] == NULL) {

Make that

       if (value == nstrings) {

to match the loop above.

Quoted 149 lines
qapi/qapi-visit-core.c +4 -3
         error_setg(errp, QERR_INVALID_PARAMETER, enum_str);
         g_free(enum_str);
         return;
@@ -380,16 +380,17 @@ static void input_type_enum(Visitor *v, const char *name, int *obj,
 }
 
 void visit_type_enum(Visitor *v, const char *name, int *obj,
-                     const char *const strings[], Error **errp)
+                     const char *const strings[], int nstrings,
+                     Error **errp)
 {
     assert(obj && strings);
     trace_visit_type_enum(v, name, obj);
     switch (v->type) {
     case VISITOR_INPUT:
-        input_type_enum(v, name, obj, strings, errp);
+        input_type_enum(v, name, obj, strings, nstrings, errp);
         break;
     case VISITOR_OUTPUT:
-        output_type_enum(v, name, obj, strings, errp);
+        output_type_enum(v, name, obj, strings, nstrings, errp);
         break;
     case VISITOR_CLONE:
         /* nothing further to do, scalar value was already copied by
backends/hostmem.c +1 -0
index 4606b73849..fc475a5387 100644
@@ -396,6 +396,7 @@ host_memory_backend_class_init(ObjectClass *oc, void *data)
         NULL, NULL, &error_abort);
     object_class_property_add_enum(oc, "policy", "HostMemPolicy",
         HostMemPolicy_lookup,
+        HOST_MEM_POLICY__MAX,
         host_memory_backend_get_policy,
         host_memory_backend_set_policy, &error_abort);
     object_class_property_add_str(oc, "id", get_id, set_id, &error_abort);
crypto/secret.c +1 -0
index 285ab7a63c..b5382cb7e3 100644
@@ -379,6 +379,7 @@ qcrypto_secret_class_init(ObjectClass *oc, void *data)
     object_class_property_add_enum(oc, "format",
                                    "QCryptoSecretFormat",
                                    QCryptoSecretFormat_lookup,
+                                   QCRYPTO_SECRET_FORMAT__MAX,
                                    qcrypto_secret_prop_get_format,
                                    qcrypto_secret_prop_set_format,
                                    NULL);
crypto/tlscreds.c +1 -0
index a8965531b6..8c060127ea 100644
@@ -234,6 +234,7 @@ qcrypto_tls_creds_class_init(ObjectClass *oc, void *data)
     object_class_property_add_enum(oc, "endpoint",
                                    "QCryptoTLSCredsEndpoint",
                                    QCryptoTLSCredsEndpoint_lookup,
+                                   QCRYPTO_TLS_CREDS_ENDPOINT__MAX,
                                    qcrypto_tls_creds_prop_get_endpoint,
                                    qcrypto_tls_creds_prop_set_endpoint,
                                    NULL);
hw/core/qdev-properties.c +9 -2
index 078fc5d239..696fed5b5b 100644
@@ -52,7 +52,8 @@ static void get_enum(Object *obj, Visitor *v, const char *name, void *opaque,
     Property *prop = opaque;
     int *ptr = qdev_get_prop_ptr(dev, prop);
 
-    visit_type_enum(v, prop->name, ptr, prop->info->enum_table, errp);
+    visit_type_enum(v, prop->name, ptr, prop->info->enum_table,
+                    prop->info->enum_table_size, errp);
 }
 
 static void set_enum(Object *obj, Visitor *v, const char *name, void *opaque,
@@ -67,7 +68,8 @@ static void set_enum(Object *obj, Visitor *v, const char *name, void *opaque,
         return;
     }
 
-    visit_type_enum(v, prop->name, ptr, prop->info->enum_table, errp);
+    visit_type_enum(v, prop->name, ptr, prop->info->enum_table,
+                    prop->info->enum_table_size, errp);
 }
 
 static void set_default_value_enum(Object *obj, const Property *prop)
@@ -586,6 +588,7 @@ const PropertyInfo qdev_prop_on_off_auto = {
     .name = "OnOffAuto",
     .description = "on/off/auto",
     .enum_table = OnOffAuto_lookup,
+    .enum_table_size = ON_OFF_AUTO__MAX,
     .get = get_enum,
     .set = set_enum,
     .set_default_value = set_default_value_enum,
@@ -598,6 +601,7 @@ QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int));
 const PropertyInfo qdev_prop_losttickpolicy = {
     .name  = "LostTickPolicy",
     .enum_table  = LostTickPolicy_lookup,
+    .enum_table_size = LOST_TICK_POLICY__MAX,
     .get   = get_enum,
     .set   = set_enum,
     .set_default_value = set_default_value_enum,
@@ -612,6 +616,7 @@ const PropertyInfo qdev_prop_blockdev_on_error = {
     .description = "Error handling policy, "
                    "report/ignore/enospc/stop/auto",
     .enum_table = BlockdevOnError_lookup,
+    .enum_table_size = BLOCKDEV_ON_ERROR__MAX,
     .get = get_enum,
     .set = set_enum,
     .set_default_value = set_default_value_enum,
@@ -626,6 +631,7 @@ const PropertyInfo qdev_prop_bios_chs_trans = {
     .description = "Logical CHS translation algorithm, "
                    "auto/none/lba/large/rechs",
     .enum_table = BiosAtaTranslation_lookup,
+    .enum_table_size = BIOS_ATA_TRANSLATION__MAX,
     .get = get_enum,
     .set = set_enum,
     .set_default_value = set_default_value_enum,
@@ -638,6 +644,7 @@ const PropertyInfo qdev_prop_fdc_drive_type = {
     .description = "FDC drive type, "
                    "144/288/120/none/auto",
     .enum_table = FloppyDriveType_lookup,
+    .enum_table_size = FLOPPY_DRIVE_TYPE__MAX,
     .get = get_enum,
     .set = set_enum,
     .set_default_value = set_default_value_enum,
net/filter.c +1 -0
index 1dfd2caa23..cf62851344 100644
@@ -180,6 +180,7 @@ static void netfilter_init(Object *obj)
                             NULL);
     object_property_add_enum(obj, "queue", "NetFilterDirection",
                              NetFilterDirection_lookup,
+                             NET_FILTER_DIRECTION__MAX,
                              netfilter_get_direction, netfilter_set_direction,
                              NULL);
     object_property_add_str(obj, "status",
qom/object.c +1 -0
index fe6e744b4d..425bae3a2a 100644
@@ -1247,6 +1247,7 @@ uint64_t object_property_get_uint(Object *obj, const char *name,
 
 typedef struct EnumProperty {
     const char * const *strings;
+    int nstrings;
     int (*get)(Object *, Error **);
     void (*set)(Object *, int, Error **);
 } EnumProperty;

qom/object.c int object_property_get_enum(Object *obj, const char *name, · line 1288

Quoted 5 lines
qom/object.c +1 -1
@@ -1284,7 +1285,7 @@ int object_property_get_enum(Object *obj, const char *name,
     visit_complete(v, &str);
     visit_free(v);
     v = string_input_visitor_new(str);
-    visit_type_enum(v, name, &ret, enumprop->strings, errp);
+    visit_type_enum(v, name, &ret, enumprop->strings, enumprop->nstrings, errp);

Long line.

Quoted 70 lines
qom/object.c +6 -2
     g_free(str);
     visit_free(v);
@@ -1950,7 +1951,7 @@ static void property_get_enum(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    visit_type_enum(v, name, &value, prop->strings, errp);
+    visit_type_enum(v, name, &value, prop->strings, prop->nstrings, errp);
 }
 
 static void property_set_enum(Object *obj, Visitor *v, const char *name,
@@ -1960,7 +1961,7 @@ static void property_set_enum(Object *obj, Visitor *v, const char *name,
     int value;
     Error *err = NULL;
 
-    visit_type_enum(v, name, &value, prop->strings, &err);
+    visit_type_enum(v, name, &value, prop->strings, prop->nstrings, &err);
     if (err) {
         error_propagate(errp, err);
         return;
@@ -1978,6 +1979,7 @@ static void property_release_enum(Object *obj, const char *name,
 void object_property_add_enum(Object *obj, const char *name,
                               const char *typename,
                               const char * const *strings,
+                              int nstrings,
                               int (*get)(Object *, Error **),
                               void (*set)(Object *, int, Error **),
                               Error **errp)
@@ -1986,6 +1988,7 @@ void object_property_add_enum(Object *obj, const char *name,
     EnumProperty *prop = g_malloc(sizeof(*prop));
 
     prop->strings = strings;
+    prop->nstrings = nstrings;
     prop->get = get;
     prop->set = set;
 
@@ -2003,6 +2006,7 @@ void object_property_add_enum(Object *obj, const char *name,
 void object_class_property_add_enum(ObjectClass *klass, const char *name,
                                     const char *typename,
                                     const char * const *strings,
+                                    int nstrings,
                                     int (*get)(Object *, Error **),
                                     void (*set)(Object *, int, Error **),
                                     Error **errp)
@@ -2011,6 +2015,7 @@ void object_class_property_add_enum(ObjectClass *klass, const char *name,
     EnumProperty *prop = g_malloc(sizeof(*prop));
 
     prop->strings = strings;
+    prop->nstrings = nstrings;
     prop->get = get;
     prop->set = set;
 
tests/check-qom-proplist.c +1 -0
index 432b66585f..1179030248 100644
@@ -143,6 +143,7 @@ static void dummy_class_init(ObjectClass *cls, void *data)
     object_class_property_add_enum(cls, "av",
                                    "DummyAnimal",
                                    dummy_animal_map,
+                                   DUMMY_LAST,
                                    dummy_get_av,
                                    dummy_set_av,
                                    NULL);
tests/test-qobject-input-visitor.c +0 -0
index 1969733971..4da5d02c35 100644

tests/test-qobject-input-visitor.c static void test_visitor_in_fail_struct_missing(TestInputVisitorData *data, · line 1116

Quoted 8 lines
tests/test-qobject-input-visitor.c +1 -1
@@ -1110,7 +1110,7 @@ static void test_visitor_in_fail_struct_missing(TestInputVisitorData *data,
     error_free_or_abort(&err);
     visit_optional(v, "optional", &present);
     g_assert(!present);
-    visit_type_enum(v, "enum", &en, EnumOne_lookup, &err);
+    visit_type_enum(v, "enum", &en, EnumOne_lookup, ENUM_ONE__MAX, &err);
     error_free_or_abort(&err);
     visit_type_int(v, "i64", &i64, &err);
     error_free_or_abort(&err);

Missing: a review of FOO_lookup[] uses outside these two, to make sure none of them fall into holes like input_type_enum() would. From the top of my head: qapi_enum_parse(). A quick grep for loops counting up to FOO__MAX additionally finds get_event_by_name() in blkdebug.c, parse_read_pattern() in quorum.c, hmp_migrate_set_capability() in hmp.c, and then I stopped looking. Most (or all?) of them should use qapi_enum_parse().

There's one patch hunk to make input_type_enum() cope with holes, one patch hunk to opportunistically simplify output_type_enum(), and all the others are for plumbing the table size to these two. That's a lot of plumbing. Can't say I like it.

Alternatives:

(1) Use a value other than NULL for holes, say ""

(2) Use a value other than NULL for the sentinel, say ""

(3) Store the length in the lookup table, i.e. change it from

    const char *const[] to struct { int n, const char *const s[] }.

The least work is probably (1). Slightly ugly.

If you do (3), please consider getting rid of the sentinel.

#15 lore raw

Re: [Qemu-devel] [PATCH 05/26] visitor: pass size of strings array to enum visitor

hi

Quoted 442 lines · Markus Armbruster
Quoted 7 lines · Marc-André Lureau

The size is known at compile time, this avoids having to compute to check array boundaries.

Additionally, the following conditional enum entry change will create "hole" in the generated _lookup tables, that should be skipped.

Quoted 15 lines
---
 include/qapi/visitor.h             |  3 ++-
 scripts/qapi-visit.py              | 10 +++++-----
 include/hw/qdev-core.h             |  1 +
 include/qom/object.h               |  4 ++++
 qapi/qapi-visit-core.c             | 23 ++++++++++++-----------
 backends/hostmem.c                 |  1 +
 crypto/secret.c                    |  1 +
 crypto/tlscreds.c                  |  1 +
 hw/core/qdev-properties.c          | 11 +++++++++--
 net/filter.c                       |  1 +
 qom/object.c                       | 11 ++++++++---
 tests/check-qom-proplist.c         |  1 +
 tests/test-qobject-input-visitor.c |  2 +-
 13 files changed, 47 insertions(+), 23 deletions(-)

No change to scripts/qapi-types.c. The generated lookup tables continue to contain the NULL sentinel. Possibly intentional, because it saves you the trouble of searching for uses of FOO_lookup[FOO__MAX].

Quoted 69 lines
include/qapi/visitor.h +2 -1
index fe9faf469f..a2d9786c52 100644
@@ -469,7 +469,8 @@ bool visit_optional(Visitor *v, const char *name, bool *present);
  * that visit_type_str() must have no unwelcome side effects.
  */
 void visit_type_enum(Visitor *v, const char *name, int *obj,
-                     const char *const strings[], Error **errp);
+                     const char *const strings[], int nstrings,
+                     Error **errp);
 
 /*
  * Check if visitor is an input visitor.
scripts/qapi-visit.py +5 -5
index bd0b742236..60850a6cdd 100644
@@ -147,17 +147,17 @@ out:
                  c_name=c_name(name), c_elt_type=element_type.c_name())
 
 
-def gen_visit_enum(name):
+def gen_visit_enum(name, prefix):
     return mcgen('''
 
 void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
 {
     int value = *obj;
-    visit_type_enum(v, name, &value, %(c_name)s_lookup, errp);
+    visit_type_enum(v, name, &value, %(c_name)s_lookup, %(c_max)s, errp);
     *obj = value;
 }
 ''',
-                 c_name=c_name(name))
+                 c_name=c_name(name), c_max=c_enum_const(name, '_MAX', prefix))
 
 
 def gen_visit_alternate(name, variants):
@@ -288,10 +288,10 @@ class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
         if not info:
             self._btin += gen_visit_decl(name, scalar=True)
             if do_builtins:
-                self.defn += gen_visit_enum(name)
+                self.defn += gen_visit_enum(name, prefix)
         else:
             self.decl += gen_visit_decl(name, scalar=True)
-            self.defn += gen_visit_enum(name)
+            self.defn += gen_visit_enum(name, prefix)
 
     def visit_array_type(self, name, info, element_type):
         decl = gen_visit_decl(name)
include/hw/qdev-core.h +1 -0
index ae317286a4..f86a0e1a75 100644
@@ -250,6 +250,7 @@ struct PropertyInfo {
     const char *name;
     const char *description;
     const char * const *enum_table;
+    int enum_table_size;
     int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
     void (*set_default_value)(Object *obj, const Property *prop);
     void (*create)(Object *obj, Property *prop, Error **errp);
include/qom/object.h +0 -0
index 1b828994fa..53d807e1e6 100644

include/qom/object.h void object_class_property_add_bool(ObjectClass *klass, const char *name, · line 1409

Quoted 4 lines
include/qom/object.h +1 -0
@@ -1406,6 +1406,8 @@ void object_class_property_add_bool(ObjectClass *klass, const char *name,
  * @obj: the object to add a property to
  * @name: the name of the property
  * @typename: the name of the enum data type
+ * @strings: an array of strings for the enum

Fixes a preexisting doc buglet.

Quoted 27 lines
include/qom/object.h +3 -0
+ * @nstrings: the size of @strings
  * @get: the getter or %NULL if the property is write-only.
  * @set: the setter or %NULL if the property is read-only
  * @errp: if an error occurs, a pointer to an area to store the error
@@ -1416,6 +1418,7 @@ void object_class_property_add_bool(ObjectClass *klass, const char *name,
 void object_property_add_enum(Object *obj, const char *name,
                               const char *typename,
                               const char * const *strings,
+                              int nstrings,
                               int (*get)(Object *, Error **),
                               void (*set)(Object *, int, Error **),
                               Error **errp);
@@ -1423,6 +1426,7 @@ void object_property_add_enum(Object *obj, const char *name,
 void object_class_property_add_enum(ObjectClass *klass, const char *name,
                                     const char *typename,
                                     const char * const *strings,
+                                    int nstrings,
                                     int (*get)(Object *, Error **),
                                     void (*set)(Object *, int, Error **),
                                     Error **errp);
qapi/qapi-visit-core.c +0 -0
index ed6d2af462..dc0b9f2cee 100644
@@ -333,14 +333,13 @@ void visit_type_null(Visitor *v, const char *name, QNull **obj,
 }
 

qapi/qapi-visit-core.c void visit_type_null(Visitor *v, const char *name, QNull **obj, · line 342

Quoted 10 lines
qapi/qapi-visit-core.c +2 -3
 static void output_type_enum(Visitor *v, const char *name, int *obj,
-                             const char *const strings[], Error **errp)
+                             const char *const strings[],
+                             int nstrings, Error **errp)
 {
-    int i = 0;
     int value = *obj;
     char *enum_str;
 
-    while (strings[i++] != NULL);

This is the computation we save.

Quoted 16 lines
qapi/qapi-visit-core.c +3 -2
-    if (value < 0 || value >= i - 1) {
+    if (value < 0 || value >= nstrings) {
         error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null");
         return;
     }
@@ -350,7 +349,8 @@ static void output_type_enum(Visitor *v, const char *name, int *obj,
 }
 
 static void input_type_enum(Visitor *v, const char *name, int *obj,
-                            const char *const strings[], Error **errp)
+                            const char *const strings[],
+                            int nstrings, Error **errp)
 {
     Error *local_err = NULL;
     int64_t value = 0;

qapi/qapi-visit-core.c static void input_type_enum(Visitor *v, const char *name, int *obj, · line 365

Quoted 4 lines
qapi/qapi-visit-core.c +0 -1
@@ -362,14 +362,14 @@ static void input_type_enum(Visitor *v, const char *name, int *obj,
         return;
     }
 
-    while (strings[value] != NULL) {

This is the predicate that becomes invalid when we put holes into strings[].

qapi/qapi-visit-core.c static void input_type_enum(Visitor *v, const char *name, int *obj, · line 366

Quoted 3 lines
qapi/qapi-visit-core.c +2 -1
-        if (strcmp(strings[value], enum_str) == 0) {
+    while (value < nstrings) {
+        if (strings[value] && strcmp(strings[value], enum_str) == 0) {

I'd prefer !strcmp().

qapi/qapi-visit-core.c static void input_type_enum(Visitor *v, const char *name, int *obj, · line 372

Quoted 7 lines
qapi/qapi-visit-core.c +1 -1
             break;
         }
         value++;
     }
 
-    if (strings[value] == NULL) {
+    if (value >= nstrings || strings[value] == NULL) {

Make that

       if (value == nstrings) {

to match the loop above.

Quoted 149 lines
qapi/qapi-visit-core.c +4 -3
         error_setg(errp, QERR_INVALID_PARAMETER, enum_str);
         g_free(enum_str);
         return;
@@ -380,16 +380,17 @@ static void input_type_enum(Visitor *v, const char *name, int *obj,
 }
 
 void visit_type_enum(Visitor *v, const char *name, int *obj,
-                     const char *const strings[], Error **errp)
+                     const char *const strings[], int nstrings,
+                     Error **errp)
 {
     assert(obj && strings);
     trace_visit_type_enum(v, name, obj);
     switch (v->type) {
     case VISITOR_INPUT:
-        input_type_enum(v, name, obj, strings, errp);
+        input_type_enum(v, name, obj, strings, nstrings, errp);
         break;
     case VISITOR_OUTPUT:
-        output_type_enum(v, name, obj, strings, errp);
+        output_type_enum(v, name, obj, strings, nstrings, errp);
         break;
     case VISITOR_CLONE:
         /* nothing further to do, scalar value was already copied by
backends/hostmem.c +1 -0
index 4606b73849..fc475a5387 100644
@@ -396,6 +396,7 @@ host_memory_backend_class_init(ObjectClass *oc, void *data)
         NULL, NULL, &error_abort);
     object_class_property_add_enum(oc, "policy", "HostMemPolicy",
         HostMemPolicy_lookup,
+        HOST_MEM_POLICY__MAX,
         host_memory_backend_get_policy,
         host_memory_backend_set_policy, &error_abort);
     object_class_property_add_str(oc, "id", get_id, set_id, &error_abort);
crypto/secret.c +1 -0
index 285ab7a63c..b5382cb7e3 100644
@@ -379,6 +379,7 @@ qcrypto_secret_class_init(ObjectClass *oc, void *data)
     object_class_property_add_enum(oc, "format",
                                    "QCryptoSecretFormat",
                                    QCryptoSecretFormat_lookup,
+                                   QCRYPTO_SECRET_FORMAT__MAX,
                                    qcrypto_secret_prop_get_format,
                                    qcrypto_secret_prop_set_format,
                                    NULL);
crypto/tlscreds.c +1 -0
index a8965531b6..8c060127ea 100644
@@ -234,6 +234,7 @@ qcrypto_tls_creds_class_init(ObjectClass *oc, void *data)
     object_class_property_add_enum(oc, "endpoint",
                                    "QCryptoTLSCredsEndpoint",
                                    QCryptoTLSCredsEndpoint_lookup,
+                                   QCRYPTO_TLS_CREDS_ENDPOINT__MAX,
                                    qcrypto_tls_creds_prop_get_endpoint,
                                    qcrypto_tls_creds_prop_set_endpoint,
                                    NULL);
hw/core/qdev-properties.c +9 -2
index 078fc5d239..696fed5b5b 100644
@@ -52,7 +52,8 @@ static void get_enum(Object *obj, Visitor *v, const char *name, void *opaque,
     Property *prop = opaque;
     int *ptr = qdev_get_prop_ptr(dev, prop);
 
-    visit_type_enum(v, prop->name, ptr, prop->info->enum_table, errp);
+    visit_type_enum(v, prop->name, ptr, prop->info->enum_table,
+                    prop->info->enum_table_size, errp);
 }
 
 static void set_enum(Object *obj, Visitor *v, const char *name, void *opaque,
@@ -67,7 +68,8 @@ static void set_enum(Object *obj, Visitor *v, const char *name, void *opaque,
         return;
     }
 
-    visit_type_enum(v, prop->name, ptr, prop->info->enum_table, errp);
+    visit_type_enum(v, prop->name, ptr, prop->info->enum_table,
+                    prop->info->enum_table_size, errp);
 }
 
 static void set_default_value_enum(Object *obj, const Property *prop)
@@ -586,6 +588,7 @@ const PropertyInfo qdev_prop_on_off_auto = {
     .name = "OnOffAuto",
     .description = "on/off/auto",
     .enum_table = OnOffAuto_lookup,
+    .enum_table_size = ON_OFF_AUTO__MAX,
     .get = get_enum,
     .set = set_enum,
     .set_default_value = set_default_value_enum,
@@ -598,6 +601,7 @@ QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int));
 const PropertyInfo qdev_prop_losttickpolicy = {
     .name  = "LostTickPolicy",
     .enum_table  = LostTickPolicy_lookup,
+    .enum_table_size = LOST_TICK_POLICY__MAX,
     .get   = get_enum,
     .set   = set_enum,
     .set_default_value = set_default_value_enum,
@@ -612,6 +616,7 @@ const PropertyInfo qdev_prop_blockdev_on_error = {
     .description = "Error handling policy, "
                    "report/ignore/enospc/stop/auto",
     .enum_table = BlockdevOnError_lookup,
+    .enum_table_size = BLOCKDEV_ON_ERROR__MAX,
     .get = get_enum,
     .set = set_enum,
     .set_default_value = set_default_value_enum,
@@ -626,6 +631,7 @@ const PropertyInfo qdev_prop_bios_chs_trans = {
     .description = "Logical CHS translation algorithm, "
                    "auto/none/lba/large/rechs",
     .enum_table = BiosAtaTranslation_lookup,
+    .enum_table_size = BIOS_ATA_TRANSLATION__MAX,
     .get = get_enum,
     .set = set_enum,
     .set_default_value = set_default_value_enum,
@@ -638,6 +644,7 @@ const PropertyInfo qdev_prop_fdc_drive_type = {
     .description = "FDC drive type, "
                    "144/288/120/none/auto",
     .enum_table = FloppyDriveType_lookup,
+    .enum_table_size = FLOPPY_DRIVE_TYPE__MAX,
     .get = get_enum,
     .set = set_enum,
     .set_default_value = set_default_value_enum,
net/filter.c +1 -0
index 1dfd2caa23..cf62851344 100644
@@ -180,6 +180,7 @@ static void netfilter_init(Object *obj)
                             NULL);
     object_property_add_enum(obj, "queue", "NetFilterDirection",
                              NetFilterDirection_lookup,
+                             NET_FILTER_DIRECTION__MAX,
                              netfilter_get_direction, netfilter_set_direction,
                              NULL);
     object_property_add_str(obj, "status",
qom/object.c +1 -0
index fe6e744b4d..425bae3a2a 100644
@@ -1247,6 +1247,7 @@ uint64_t object_property_get_uint(Object *obj, const char *name,
 
 typedef struct EnumProperty {
     const char * const *strings;
+    int nstrings;
     int (*get)(Object *, Error **);
     void (*set)(Object *, int, Error **);
 } EnumProperty;

qom/object.c int object_property_get_enum(Object *obj, const char *name, · line 1288

Quoted 5 lines
qom/object.c +1 -1
@@ -1284,7 +1285,7 @@ int object_property_get_enum(Object *obj, const char *name,
     visit_complete(v, &str);
     visit_free(v);
     v = string_input_visitor_new(str);
-    visit_type_enum(v, name, &ret, enumprop->strings, errp);
+    visit_type_enum(v, name, &ret, enumprop->strings, enumprop->nstrings, errp);

Long line.

Quoted 70 lines
qom/object.c +6 -2
     g_free(str);
     visit_free(v);
@@ -1950,7 +1951,7 @@ static void property_get_enum(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    visit_type_enum(v, name, &value, prop->strings, errp);
+    visit_type_enum(v, name, &value, prop->strings, prop->nstrings, errp);
 }
 
 static void property_set_enum(Object *obj, Visitor *v, const char *name,
@@ -1960,7 +1961,7 @@ static void property_set_enum(Object *obj, Visitor *v, const char *name,
     int value;
     Error *err = NULL;
 
-    visit_type_enum(v, name, &value, prop->strings, &err);
+    visit_type_enum(v, name, &value, prop->strings, prop->nstrings, &err);
     if (err) {
         error_propagate(errp, err);
         return;
@@ -1978,6 +1979,7 @@ static void property_release_enum(Object *obj, const char *name,
 void object_property_add_enum(Object *obj, const char *name,
                               const char *typename,
                               const char * const *strings,
+                              int nstrings,
                               int (*get)(Object *, Error **),
                               void (*set)(Object *, int, Error **),
                               Error **errp)
@@ -1986,6 +1988,7 @@ void object_property_add_enum(Object *obj, const char *name,
     EnumProperty *prop = g_malloc(sizeof(*prop));
 
     prop->strings = strings;
+    prop->nstrings = nstrings;
     prop->get = get;
     prop->set = set;
 
@@ -2003,6 +2006,7 @@ void object_property_add_enum(Object *obj, const char *name,
 void object_class_property_add_enum(ObjectClass *klass, const char *name,
                                     const char *typename,
                                     const char * const *strings,
+                                    int nstrings,
                                     int (*get)(Object *, Error **),
                                     void (*set)(Object *, int, Error **),
                                     Error **errp)
@@ -2011,6 +2015,7 @@ void object_class_property_add_enum(ObjectClass *klass, const char *name,
     EnumProperty *prop = g_malloc(sizeof(*prop));
 
     prop->strings = strings;
+    prop->nstrings = nstrings;
     prop->get = get;
     prop->set = set;
 
tests/check-qom-proplist.c +1 -0
index 432b66585f..1179030248 100644
@@ -143,6 +143,7 @@ static void dummy_class_init(ObjectClass *cls, void *data)
     object_class_property_add_enum(cls, "av",
                                    "DummyAnimal",
                                    dummy_animal_map,
+                                   DUMMY_LAST,
                                    dummy_get_av,
                                    dummy_set_av,
                                    NULL);
tests/test-qobject-input-visitor.c +0 -0
index 1969733971..4da5d02c35 100644

tests/test-qobject-input-visitor.c static void test_visitor_in_fail_struct_missing(TestInputVisitorData *data, · line 1116

Quoted 8 lines
tests/test-qobject-input-visitor.c +1 -1
@@ -1110,7 +1110,7 @@ static void test_visitor_in_fail_struct_missing(TestInputVisitorData *data,
     error_free_or_abort(&err);
     visit_optional(v, "optional", &present);
     g_assert(!present);
-    visit_type_enum(v, "enum", &en, EnumOne_lookup, &err);
+    visit_type_enum(v, "enum", &en, EnumOne_lookup, ENUM_ONE__MAX, &err);
     error_free_or_abort(&err);
     visit_type_int(v, "i64", &i64, &err);
     error_free_or_abort(&err);

Missing: a review of FOO_lookup[] uses outside these two, to make sure none of them fall into holes like input_type_enum() would. From the top of my head: qapi_enum_parse(). A quick grep for loops counting up to FOO__MAX additionally finds get_event_by_name() in blkdebug.c, parse_read_pattern() in quorum.c, hmp_migrate_set_capability() in hmp.c, and then I stopped looking. Most (or all?) of them should use qapi_enum_parse().

There's one patch hunk to make input_type_enum() cope with holes, one patch hunk to opportunistically simplify output_type_enum(), and all the others are for plumbing the table size to these two. That's a lot of plumbing. Can't say I like it.

Alternatives:

(1) Use a value other than NULL for holes, say ""

Quoted 9 lines

(2) Use a value other than NULL for the sentinel, say ""

(3) Store the length in the lookup table, i.e. change it from

    const char *const[] to struct { int n, const char *const s[] }.

The least work is probably (1). Slightly ugly.

If you do (3), please consider getting rid of the sentinel.

I have done (3)

Signature · 1 line
Marc-André Lureau
#16 lore raw

PATCH 6/26 qapi2texi: minor python code simplification

In reply to Marc-André Lureau
---
 scripts/qapi2texi.py | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
scripts/qapi2texi.py +2 -3
index 9e015002ef..639eb1d042 100755
@@ -136,10 +136,9 @@ def texi_enum_value(value):
 def texi_member(member, suffix=''):
     """Format a table of members item for an object type member"""
     typ = member.type.doc_type()
-    return '@item @code{%s%s%s}%s%s\n' % (
+    return '@item @code{%s%s}%s%s\n' % (
         member.name,
-        ': ' if typ else '',
-        typ if typ else '',
+        ': %s' % typ if typ else '',
         ' (optional)' if member.optional else '',
         suffix)
 
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#17 lore raw

Re: [Qemu-devel] [PATCH 06/26] qapi2texi: minor python code simplification

Quoted 11 lines · Marc-André Lureau
---
 scripts/qapi2texi.py | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
scripts/qapi2texi.py +0 -0
index 9e015002ef..639eb1d042 100755
@@ -136,10 +136,9 @@ def texi_enum_value(value):
 def texi_member(member, suffix=''):

scripts/qapi2texi.py def texi_enum_value(value): · line 143

Quoted 10 lines
scripts/qapi2texi.py +2 -3
     """Format a table of members item for an object type member"""
     typ = member.type.doc_type()
-    return '@item @code{%s%s%s}%s%s\n' % (
+    return '@item @code{%s%s}%s%s\n' % (
         member.name,
-        ': ' if typ else '',
-        typ if typ else '',
+        ': %s' % typ if typ else '',
         ' (optional)' if member.optional else '',
         suffix)

Is nested interpolation really simpler?

#18 lore raw

PATCH 7/26 qapi: add 'if' condition on top-level schema elements

In reply to Marc-André Lureau

Add 'if' c-preprocessor condition on top-level schema elements:
struct, enum, union, alternate, command, event.

Variants objects types are created outside of #if blocks, since they may be shared by various types. Even if unused, this shouldn't be an issue, since those are internal types.

Note that there is no checks in qapi scripts to verify that elements have compatible conditions (ex: if-struct used by a if-foo command). This may thus fail at C build time if they don't share the same subset of conditions.

---
 scripts/qapi.py                         | 153 +++++++++++++++++++++++++-------
 scripts/qapi-commands.py                |   4 +-
 scripts/qapi-event.py                   |   4 +-
 scripts/qapi-introspect.py              |  46 ++++++----
 scripts/qapi-types.py                   |  51 +++++++----
 scripts/qapi-visit.py                   |  13 ++-
 scripts/qapi2texi.py                    |  10 +--
 tests/Makefile.include                  |   1 +
 tests/qapi-schema/bad-if.err            |   1 +
 tests/qapi-schema/bad-if.exit           |   1 +
 tests/qapi-schema/bad-if.json           |   3 +
 tests/qapi-schema/bad-if.out            |   0
 tests/qapi-schema/qapi-schema-test.json |  20 +++++
 tests/qapi-schema/qapi-schema-test.out  |  31 +++++++
 tests/qapi-schema/test-qapi.py          |  21 +++--
 15 files changed, 272 insertions(+), 87 deletions(-)
 create mode 100644 tests/qapi-schema/bad-if.err
 create mode 100644 tests/qapi-schema/bad-if.exit
 create mode 100644 tests/qapi-schema/bad-if.json
 create mode 100644 tests/qapi-schema/bad-if.out
scripts/qapi.py +120 -33
index 4ecc19e944..79ba1e93da 100644
@@ -639,6 +639,16 @@ def add_name(name, info, meta, implicit=False):
     all_names[name] = meta
 
 
+def check_if(expr, info):
+    ifcond = expr.get('if')
+    if not ifcond or isinstance(ifcond, str):
+        return
+    if (not isinstance(ifcond, list) or
+        any([not isinstance(elt, str) for elt in ifcond])):
+        raise QAPISemError(info, "'if' condition requires a string or "
+                           "a list of string")
+
+
 def check_type(info, source, value, allow_array=False,
                allow_dict=False, allow_optional=False,
                allow_metas=[]):
@@ -865,6 +875,7 @@ def check_keys(expr_elem, meta, required, optional=[]):
     expr = expr_elem['expr']
     info = expr_elem['info']
     name = expr[meta]
+    optional.append('if')
     if not isinstance(name, str):
         raise QAPISemError(info, "'%s' key must have a string value" % meta)
     required = required + [meta]
@@ -880,6 +891,8 @@ def check_keys(expr_elem, meta, required, optional=[]):
             raise QAPISemError(info,
                                "'%s' of %s '%s' should only use true value"
                                % (key, meta, name))
+        if key == 'if':
+            check_if(expr, info)
     for key in required:
         if key not in expr:
             raise QAPISemError(info, "Key '%s' is missing from %s '%s'"
@@ -989,6 +1002,10 @@ class QAPISchemaEntity(object):
         # such place).
         self.info = info
         self.doc = doc
+        self.ifcond = None
+
+    def set_ifcond(self, ifcond):
+        self.ifcond = ifcond
 
     def c_name(self):
         return c_name(self.name)
@@ -1017,26 +1034,26 @@ class QAPISchemaVisitor(object):
     def visit_builtin_type(self, name, info, json_type):
         pass
 
-    def visit_enum_type(self, name, info, values, prefix):
+    def visit_enum_type(self, name, info, values, prefix, ifcond):
         pass
 
-    def visit_array_type(self, name, info, element_type):
+    def visit_array_type(self, name, info, element_type, ifcond):
         pass
 
-    def visit_object_type(self, name, info, base, members, variants):
+    def visit_object_type(self, name, info, base, members, variants, ifcond):
         pass
 
-    def visit_object_type_flat(self, name, info, members, variants):
+    def visit_object_type_flat(self, name, info, members, variants, ifcond):
         pass
 
-    def visit_alternate_type(self, name, info, variants):
+    def visit_alternate_type(self, name, info, variants, ifcond):
         pass
 
     def visit_command(self, name, info, arg_type, ret_type,
-                      gen, success_response, boxed):
+                      gen, success_response, boxed, ifcond):
         pass
 
-    def visit_event(self, name, info, arg_type, boxed):
+    def visit_event(self, name, info, arg_type, boxed, ifcond):
         pass
 
 
@@ -1136,7 +1153,7 @@ class QAPISchemaEnumType(QAPISchemaType):
 
     def visit(self, visitor):
         visitor.visit_enum_type(self.name, self.info,
-                                self.member_names(), self.prefix)
+                                self.member_names(), self.prefix, self.ifcond)
 
 
 class QAPISchemaArrayType(QAPISchemaType):
@@ -1149,6 +1166,7 @@ class QAPISchemaArrayType(QAPISchemaType):
     def check(self, schema):
         self.element_type = schema.lookup_type(self._element_type_name)
         assert self.element_type
+        self.ifcond = self.element_type.ifcond
 
     def is_implicit(self):
         return True
@@ -1166,7 +1184,8 @@ class QAPISchemaArrayType(QAPISchemaType):
         return 'array of ' + elt_doc_type
 
     def visit(self, visitor):
-        visitor.visit_array_type(self.name, self.info, self.element_type)
+        visitor.visit_array_type(self.name, self.info, self.element_type,
+                                 self.ifcond)
 
 
 class QAPISchemaObjectType(QAPISchemaType):
@@ -1247,9 +1266,10 @@ class QAPISchemaObjectType(QAPISchemaType):
 
     def visit(self, visitor):
         visitor.visit_object_type(self.name, self.info,
-                                  self.base, self.local_members, self.variants)
+                                  self.base, self.local_members, self.variants,
+                                  self.ifcond)
         visitor.visit_object_type_flat(self.name, self.info,
-                                       self.members, self.variants)
+                                       self.members, self.variants, self.ifcond)
 
 
 class QAPISchemaMember(object):
@@ -1392,7 +1412,8 @@ class QAPISchemaAlternateType(QAPISchemaType):
         return 'value'
 
     def visit(self, visitor):
-        visitor.visit_alternate_type(self.name, self.info, self.variants)
+        visitor.visit_alternate_type(self.name, self.info,
+                                     self.variants, self.ifcond)
 
     def is_empty(self):
         return False
@@ -1434,7 +1455,8 @@ class QAPISchemaCommand(QAPISchemaEntity):
     def visit(self, visitor):
         visitor.visit_command(self.name, self.info,
                               self.arg_type, self.ret_type,
-                              self.gen, self.success_response, self.boxed)
+                              self.gen, self.success_response, self.boxed,
+                              self.ifcond)
 
 
 class QAPISchemaEvent(QAPISchemaEntity):
@@ -1462,7 +1484,8 @@ class QAPISchemaEvent(QAPISchemaEntity):
             raise QAPISemError(self.info, "Use of 'boxed' requires 'data'")
 
     def visit(self, visitor):
-        visitor.visit_event(self.name, self.info, self.arg_type, self.boxed)
+        visitor.visit_event(self.name, self.info, self.arg_type, self.boxed,
+                            self.ifcond)
 
 
 class QAPISchema(object):
@@ -1481,11 +1504,12 @@ class QAPISchema(object):
             print >>sys.stderr, err
             exit(1)
 
-    def _def_entity(self, ent):
+    def _def_entity(self, ent, ifcond=None):
         # Only the predefined types are allowed to not have info
         assert ent.info or self._predefining
         assert ent.name not in self._entity_dict
         self._entity_dict[ent.name] = ent
+        ent.set_ifcond(ifcond)
 
     def lookup_entity(self, name, typ=None):
         ent = self._entity_dict.get(name)
@@ -1534,11 +1558,11 @@ class QAPISchema(object):
     def _make_enum_members(self, values):
         return [QAPISchemaMember(v) for v in values]
 
-    def _make_implicit_enum_type(self, name, info, values):
+    def _make_implicit_enum_type(self, name, info, values, ifcond):
         # See also QAPISchemaObjectTypeMember._pretty_owner()
         name = name + 'Kind'   # Use namespace reserved by add_name()
         self._def_entity(QAPISchemaEnumType(
-            name, info, None, self._make_enum_members(values), None))
+            name, info, None, self._make_enum_members(values), None), ifcond)
         return name
 
     def _make_array_type(self, element_type, info):
@@ -1547,22 +1571,26 @@ class QAPISchema(object):
             self._def_entity(QAPISchemaArrayType(name, info, element_type))
         return name
 
-    def _make_implicit_object_type(self, name, info, doc, role, members):
+    def _make_implicit_object_type(self, name, info, doc, role, members,
+                                   ifcond=None):
         if not members:
             return None
         # See also QAPISchemaObjectTypeMember._pretty_owner()
         name = 'q_obj_%s-%s' % (name, role)
-        if not self.lookup_entity(name, QAPISchemaObjectType):
+        if self.lookup_entity(name, QAPISchemaObjectType):
+            assert ifcond is None
+        else:
             self._def_entity(QAPISchemaObjectType(name, info, doc, None,
-                                                  members, None))
+                                                  members, None), ifcond)
         return name
 
     def _def_enum_type(self, expr, info, doc):
         name = expr['enum']
         data = expr['data']
         prefix = expr.get('prefix')
-        self._def_entity(QAPISchemaEnumType(
-            name, info, doc, self._make_enum_members(data), prefix))
+        return self._def_entity(QAPISchemaEnumType(
+            name, info, doc, self._make_enum_members(data), prefix),
+                                expr.get('if'))
 
     def _make_member(self, name, typ, info):
         optional = False
@@ -1584,7 +1612,8 @@ class QAPISchema(object):
         data = expr['data']
         self._def_entity(QAPISchemaObjectType(name, info, doc, base,
                                               self._make_members(data, info),
-                                              None))
+                                              None),
+                         expr.get('if'))
 
     def _make_variant(self, case, typ):
         return QAPISchemaObjectTypeVariant(case, typ)
@@ -1593,8 +1622,10 @@ class QAPISchema(object):
         if isinstance(typ, list):
             assert len(typ) == 1
             typ = self._make_array_type(typ[0], info)
+        type_entity = self.lookup_type(typ)
         typ = self._make_implicit_object_type(
-            typ, info, None, 'wrapper', [self._make_member('data', typ, info)])
+            typ, info, None, 'wrapper',
+            [self._make_member('data', typ, info)], type_entity.ifcond)
         return QAPISchemaObjectTypeVariant(case, typ)
 
     def _def_union_type(self, expr, info, doc):
@@ -1604,8 +1635,9 @@ class QAPISchema(object):
         tag_name = expr.get('discriminator')
         tag_member = None
         if isinstance(base, dict):
-            base = (self._make_implicit_object_type(
-                name, info, doc, 'base', self._make_members(base, info)))
+            base = self._make_implicit_object_type(
+                name, info, doc, 'base', self._make_members(base, info),
+                expr.get('if'))
         if tag_name:
             variants = [self._make_variant(key, value)
                         for (key, value) in data.iteritems()]
@@ -1614,14 +1646,16 @@ class QAPISchema(object):
             variants = [self._make_simple_variant(key, value, info)
                         for (key, value) in data.iteritems()]
             typ = self._make_implicit_enum_type(name, info,
-                                                [v.name for v in variants])
+                                                [v.name for v in variants],
+                                                expr.get('if'))
             tag_member = QAPISchemaObjectTypeMember('type', typ, False)
             members = [tag_member]
         self._def_entity(
             QAPISchemaObjectType(name, info, doc, base, members,
                                  QAPISchemaObjectTypeVariants(tag_name,
                                                               tag_member,
-                                                              variants)))
+                                                              variants)),
+            expr.get('if'))
 
     def _def_alternate_type(self, expr, info, doc):
         name = expr['alternate']
@@ -1633,7 +1667,8 @@ class QAPISchema(object):
             QAPISchemaAlternateType(name, info, doc,
                                     QAPISchemaObjectTypeVariants(None,
                                                                  tag_member,
-                                                                 variants)))
+                                                                 variants)),
+            expr.get('if'))
 
     def _def_command(self, expr, info, doc):
         name = expr['command']
@@ -1644,12 +1679,14 @@ class QAPISchema(object):
         boxed = expr.get('boxed', False)
         if isinstance(data, OrderedDict):
             data = self._make_implicit_object_type(
-                name, info, doc, 'arg', self._make_members(data, info))
+                name, info, doc, 'arg', self._make_members(data, info),
+                expr.get('if'))
         if isinstance(rets, list):
             assert len(rets) == 1
             rets = self._make_array_type(rets[0], info)
         self._def_entity(QAPISchemaCommand(name, info, doc, data, rets,
-                                           gen, success_response, boxed))
+                                           gen, success_response, boxed),
+                         expr.get('if'))
 
     def _def_event(self, expr, info, doc):
         name = expr['event']
@@ -1657,8 +1694,10 @@ class QAPISchema(object):
         boxed = expr.get('boxed', False)
         if isinstance(data, OrderedDict):
             data = self._make_implicit_object_type(
-                name, info, doc, 'arg', self._make_members(data, info))
-        self._def_entity(QAPISchemaEvent(name, info, doc, data, boxed))
+                name, info, doc, 'arg', self._make_members(data, info),
+                expr.get('if'))
+        self._def_entity(QAPISchemaEvent(name, info, doc, data, boxed),
+                         expr.get('if'))
 
     def _def_exprs(self):
         for expr_elem in self.exprs:
@@ -1848,6 +1887,54 @@ def guardend(name):
                  name=guardname(name))
 
 
+def gen_if(ifcond, func=''):
+    if not ifcond:
+        return ''
+    if isinstance(ifcond, str):
+        ifcond = [ifcond]
+    ret = '\n'
+    for ifc in ifcond:
+        ret += mcgen('#if %(ifcond)s /* %(func)s */\n', ifcond=ifc, func=func)
+    ret += '\n'
+    return ret
+
+
+def gen_endif(ifcond, func=''):
+    if not ifcond:
+        return ''
+    if isinstance(ifcond, str):
+        ifcond = [ifcond]
+    ret = '\n'
+    for ifc in reversed(ifcond):
+        ret += mcgen('#endif /* %(ifcond)s %(func)s */\n',
+                     ifcond=ifc, func=func)
+    ret += '\n'
+    return ret
+
+
+# wrap a method to add #if / #endif to generated code
+# self must have 'if_members' listing the attributes to wrap
+# the last argument of the wrapped function must be the 'ifcond'
+def if_wrap(func):
+    def func_wrapper(self, *args, **kwargs):
+        funcname = self.__class__.__name__ + '.' + func.__name__
+        ifcond = args[-1]
+        save = {}
+        for mem in self.if_members:
+            save[mem] = getattr(self, mem)
+        func(self, *args, **kwargs)
+        for mem, val in save.items():
+            newval = getattr(self, mem)
+            if newval != val:
+                assert newval.startswith(val)
+                newval = newval[len(val):]
+                val += gen_if(ifcond, funcname)
+                val += newval
+                val += gen_endif(ifcond, funcname)
+            setattr(self, mem, val)
+
+    return func_wrapper
+
 def gen_enum_lookup(name, values, prefix=None):
     ret = mcgen('''
 
scripts/qapi-commands.py +3 -1
index 974d0a4a80..19b1bb9b88 100644
@@ -228,6 +228,7 @@ class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
         self.defn = None
         self._regy = None
         self._visited_ret_types = None
+        self.if_members = ['decl', 'defn', '_regy']
 
     def visit_begin(self, schema):
         self.decl = ''
@@ -240,8 +241,9 @@ class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
         self._regy = None
         self._visited_ret_types = None
 
+    @if_wrap
     def visit_command(self, name, info, arg_type, ret_type,
-                      gen, success_response, boxed):
+                      gen, success_response, boxed, ifcond):
         if not gen:
             return
         self.decl += gen_command_decl(name, arg_type, boxed, ret_type)
scripts/qapi-event.py +3 -1
index bcbef1035f..cad9ece790 100644
@@ -152,6 +152,7 @@ class QAPISchemaGenEventVisitor(QAPISchemaVisitor):
         self.decl = None
         self.defn = None
         self._event_names = None
+        self.if_members = ['decl', 'defn']
 
     def visit_begin(self, schema):
         self.decl = ''
@@ -163,7 +164,8 @@ class QAPISchemaGenEventVisitor(QAPISchemaVisitor):
         self.defn += gen_enum_lookup(event_enum_name, self._event_names)
         self._event_names = None
 
-    def visit_event(self, name, info, arg_type, boxed):
+    @if_wrap
+    def visit_event(self, name, info, arg_type, boxed, ifcond):
         self.decl += gen_event_send_decl(name, arg_type, boxed)
         self.defn += gen_event_send(name, arg_type, boxed)
         self._event_names.append(name)
scripts/qapi-introspect.py +27 -19

105 lines not on this page · raw message

scripts/qapi-types.py +32 -19

100 lines not on this page · raw message

scripts/qapi-visit.py +9 -4
index 60850a6cdd..335407d078 100644
@@ -267,6 +267,7 @@ class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
         self.decl = None
         self.defn = None
         self._btin = None
+        self.if_members = ['decl', 'defn', '_btin']
 
     def visit_begin(self, schema):
         self.decl = ''
@@ -282,7 +283,8 @@ class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
         self.decl = self._btin + self.decl
         self._btin = None
 
-    def visit_enum_type(self, name, info, values, prefix):
+    @if_wrap
+    def visit_enum_type(self, name, info, values, prefix, ifcond):
         # Special case for our lone builtin enum type
         # TODO use something cleaner than existence of info
         if not info:
@@ -293,7 +295,8 @@ class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
             self.decl += gen_visit_decl(name, scalar=True)
             self.defn += gen_visit_enum(name, prefix)
 
-    def visit_array_type(self, name, info, element_type):
+    @if_wrap
+    def visit_array_type(self, name, info, element_type, ifcond):
         decl = gen_visit_decl(name)
         defn = gen_visit_list(name, element_type)
         if isinstance(element_type, QAPISchemaBuiltinType):
@@ -304,7 +307,8 @@ class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
             self.decl += decl
             self.defn += defn
 
-    def visit_object_type(self, name, info, base, members, variants):
+    @if_wrap
+    def visit_object_type(self, name, info, base, members, variants, ifcond):
         # Nothing to do for the special empty builtin
         if name == 'q_empty':
             return
@@ -317,7 +321,8 @@ class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
             self.decl += gen_visit_decl(name)
             self.defn += gen_visit_object(name, base, members, variants)
 
-    def visit_alternate_type(self, name, info, variants):
+    @if_wrap
+    def visit_alternate_type(self, name, info, variants, ifcond):
         self.decl += gen_visit_decl(name)
         self.defn += gen_visit_alternate(name, variants)
 
scripts/qapi2texi.py +5 -5
index 639eb1d042..5aa2f48bfd 100755
@@ -207,7 +207,7 @@ class QAPISchemaGenDocVisitor(qapi.QAPISchemaVisitor):
     def visit_begin(self, schema):
         self.out = ''
 
-    def visit_enum_type(self, name, info, values, prefix):
+    def visit_enum_type(self, name, info, values, prefix, ifcond):
         doc = self.cur_doc
         if self.out:
             self.out += '\n'
@@ -216,7 +216,7 @@ class QAPISchemaGenDocVisitor(qapi.QAPISchemaVisitor):
                              body=texi_entity(doc, 'Values',
                                               member_func=texi_enum_value))
 
-    def visit_object_type(self, name, info, base, members, variants):
+    def visit_object_type(self, name, info, base, members, variants, ifcond):
         doc = self.cur_doc
         if base and base.is_implicit():
             base = None
@@ -226,7 +226,7 @@ class QAPISchemaGenDocVisitor(qapi.QAPISchemaVisitor):
                              name=doc.symbol,
                              body=texi_entity(doc, 'Members', base, variants))
 
-    def visit_alternate_type(self, name, info, variants):
+    def visit_alternate_type(self, name, info, variants, ifcond):
         doc = self.cur_doc
         if self.out:
             self.out += '\n'
@@ -235,7 +235,7 @@ class QAPISchemaGenDocVisitor(qapi.QAPISchemaVisitor):
                              body=texi_entity(doc, 'Members'))
 
     def visit_command(self, name, info, arg_type, ret_type,
-                      gen, success_response, boxed):
+                      gen, success_response, boxed, ifcond):
         doc = self.cur_doc
         if self.out:
             self.out += '\n'
@@ -249,7 +249,7 @@ class QAPISchemaGenDocVisitor(qapi.QAPISchemaVisitor):
                             name=doc.symbol,
                             body=body)
 
-    def visit_event(self, name, info, arg_type, boxed):
+    def visit_event(self, name, info, arg_type, boxed, ifcond):
         doc = self.cur_doc
         if self.out:
             self.out += '\n'
tests/Makefile.include +1 -0
index 960ab8c6dd..0fc7088b2c 100644
@@ -373,6 +373,7 @@ qapi-schema += args-unknown.json
 qapi-schema += bad-base.json
 qapi-schema += bad-data.json
 qapi-schema += bad-ident.json
+qapi-schema += bad-if.json
 qapi-schema += bad-type-bool.json
 qapi-schema += bad-type-dict.json
 qapi-schema += bad-type-int.json
tests/qapi-schema/bad-if.err new file +1 -0
new file mode 100644
index 0000000000..8054fbb143
@@ -0,0 +1 @@
+tests/qapi-schema/bad-if.json:2: 'if' condition requires a string or a list of string
tests/qapi-schema/bad-if.exit new file +1 -0
new file mode 100644
index 0000000000..d00491fd7e
@@ -0,0 +1 @@
+1
tests/qapi-schema/bad-if.json new file +3 -0
new file mode 100644
index 0000000000..3edd1a0bf2
@@ -0,0 +1,3 @@
+# check invalid 'if' type
+{ 'struct': 'TestIfStruct', 'data': { 'foo': 'int' },
+  'if': { 'value': 'defined(TEST_IF_STRUCT)' } }
tests/qapi-schema/bad-if.out new file +0 -0
new file mode 100644
index 0000000000..e69de29bb2
tests/qapi-schema/qapi-schema-test.json +20 -0
index c72dbd8050..dc2c444fc1 100644
@@ -188,3 +188,23 @@
   'data': { 'a': ['__org.qemu_x-Enum'], 'b': ['__org.qemu_x-Struct'],
             'c': '__org.qemu_x-Union2', 'd': '__org.qemu_x-Alt' },
   'returns': '__org.qemu_x-Union1' }
+
+# test 'if' condition handling
+
+{ 'struct': 'TestIfStruct', 'data': { 'foo': 'int' },
+  'if': 'defined(TEST_IF_STRUCT)' }
+
+{ 'enum': 'TestIfEnum', 'data': [ 'foo', 'bar' ],
+  'if': 'defined(TEST_IF_ENUM)' }
+
+{ 'union': 'TestIfUnion', 'data': { 'foo': 'TestStruct' },
+  'if': 'defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)' }
+
+{ 'alternate': 'TestIfAlternate', 'data': { 'foo': 'int', 'bar': 'TestStruct' },
+  'if': 'defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)' }
+
+{ 'command': 'TestIfCmd', 'data': { 'foo': 'TestIfStruct' },
+  'if': 'defined(TEST_IF_CMD) && defined(TEST_IF_STRUCT)' }
+
+{ 'event': 'TestIfEvent', 'data': { 'foo': 'TestIfStruct' },
+  'if': 'defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)' }
tests/qapi-schema/qapi-schema-test.out +31 -0
index 3b1e9082d3..fc5fd25f1b 100644
@@ -52,6 +52,29 @@ enum QEnumTwo ['value1', 'value2']
     prefix QENUM_TWO
 enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool']
     prefix QTYPE
+alternate TestIfAlternate
+    tag type
+    case foo: int
+    case bar: TestStruct
+    if defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)
+command TestIfCmd q_obj_TestIfCmd-arg -> None
+   gen=True success_response=True boxed=False
+    if defined(TEST_IF_CMD) && defined(TEST_IF_STRUCT)
+enum TestIfEnum ['foo', 'bar']
+    if defined(TEST_IF_ENUM)
+event TestIfEvent q_obj_TestIfEvent-arg
+   boxed=False
+    if defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)
+object TestIfStruct
+    member foo: int optional=False
+    if defined(TEST_IF_STRUCT)
+object TestIfUnion
+    member type: TestIfUnionKind optional=False
+    tag type
+    case foo: q_obj_TestStruct-wrapper
+    if defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)
+enum TestIfUnionKind ['foo']
+    if defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)
 object TestStruct
     member integer: int optional=False
     member boolean: bool optional=False
@@ -172,6 +195,14 @@ object q_obj_EVENT_D-arg
     member b: str optional=False
     member c: str optional=True
     member enum3: EnumOne optional=True
+object q_obj_TestIfCmd-arg
+    member foo: TestIfStruct optional=False
+    if defined(TEST_IF_CMD) && defined(TEST_IF_STRUCT)
+object q_obj_TestIfEvent-arg
+    member foo: TestIfStruct optional=False
+    if defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)
+object q_obj_TestStruct-wrapper
+    member data: TestStruct optional=False
 object q_obj_UserDefFlatUnion2-base
     member integer: int optional=True
     member string: str optional=False
tests/qapi-schema/test-qapi.py +16 -5
index c7724d3437..17fd975812 100644
@@ -17,12 +17,13 @@ import sys
 
 
 class QAPISchemaTestVisitor(QAPISchemaVisitor):
-    def visit_enum_type(self, name, info, values, prefix):
+    def visit_enum_type(self, name, info, values, prefix, ifcond):
         print 'enum %s %s' % (name, values)
         if prefix:
             print '    prefix %s' % prefix
+        self._print_if(ifcond)
 
-    def visit_object_type(self, name, info, base, members, variants):
+    def visit_object_type(self, name, info, base, members, variants, ifcond):
         print 'object %s' % name
         if base:
             print '    base %s' % base.name
@@ -30,21 +31,25 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor):
             print '    member %s: %s optional=%s' % \
                 (m.name, m.type.name, m.optional)
         self._print_variants(variants)
+        self._print_if(ifcond)
 
-    def visit_alternate_type(self, name, info, variants):
+    def visit_alternate_type(self, name, info, variants, ifcond):
         print 'alternate %s' % name
         self._print_variants(variants)
+        self._print_if(ifcond)
 
     def visit_command(self, name, info, arg_type, ret_type,
-                      gen, success_response, boxed):
+                      gen, success_response, boxed, ifcond):
         print 'command %s %s -> %s' % \
             (name, arg_type and arg_type.name, ret_type and ret_type.name)
         print '   gen=%s success_response=%s boxed=%s' % \
             (gen, success_response, boxed)
+        self._print_if(ifcond)
 
-    def visit_event(self, name, info, arg_type, boxed):
+    def visit_event(self, name, info, arg_type, boxed, ifcond):
         print 'event %s %s' % (name, arg_type and arg_type.name)
         print '   boxed=%s' % boxed
+        self._print_if(ifcond)
 
     @staticmethod
     def _print_variants(variants):
@@ -53,6 +58,12 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor):
             for v in variants.variants:
                 print '    case %s: %s' % (v.name, v.type.name)
 
+    @staticmethod
+    def _print_if(ifcond):
+        if ifcond:
+            print '    if %s' % ifcond
+
+
 schema = QAPISchema(sys.argv[1])
 schema.visit(QAPISchemaTestVisitor())
 
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#19 lore raw

Re: [Qemu-devel] [PATCH 07/26] qapi: add 'if' condition on top-level schema elements

Quoted 2 lines · Marc-André Lureau

Add 'if' c-preprocessor condition on top-level schema elements:
struct, enum, union, alternate, command, event.

An example would be useful here. Your cover letter has nice ones, would be a shame not to preserve them for posterity in the commit log.

Quoted 1 line

Variants objects types are created outside of #if blocks, since they

What are "variants objects types"?

Quoted 4 lines

may be shared by various types. Even if unused, this shouldn't be an issue, since those are internal types.

Note that there is no checks in qapi scripts to verify that elements

there are

Quoted 3 lines

have compatible conditions (ex: if-struct used by a if-foo command). This may thus fail at C build time if they don't share the same subset of conditions.

Fair enough.

Quoted 29 lines
---
 scripts/qapi.py                         | 153 +++++++++++++++++++++++++-------
 scripts/qapi-commands.py                |   4 +-
 scripts/qapi-event.py                   |   4 +-
 scripts/qapi-introspect.py              |  46 ++++++----
 scripts/qapi-types.py                   |  51 +++++++----
 scripts/qapi-visit.py                   |  13 ++-
 scripts/qapi2texi.py                    |  10 +--
 tests/Makefile.include                  |   1 +
 tests/qapi-schema/bad-if.err            |   1 +
 tests/qapi-schema/bad-if.exit           |   1 +
 tests/qapi-schema/bad-if.json           |   3 +
 tests/qapi-schema/bad-if.out            |   0
 tests/qapi-schema/qapi-schema-test.json |  20 +++++
 tests/qapi-schema/qapi-schema-test.out  |  31 +++++++
 tests/qapi-schema/test-qapi.py          |  21 +++--
 15 files changed, 272 insertions(+), 87 deletions(-)
 create mode 100644 tests/qapi-schema/bad-if.err
 create mode 100644 tests/qapi-schema/bad-if.exit
 create mode 100644 tests/qapi-schema/bad-if.json
 create mode 100644 tests/qapi-schema/bad-if.out
scripts/qapi.py +0 -0
index 4ecc19e944..79ba1e93da 100644
@@ -639,6 +639,16 @@ def add_name(name, info, meta, implicit=False):
     all_names[name] = meta

scripts/qapi.py def add_name(name, info, meta, implicit=False): · line 649

Quoted 10 lines
scripts/qapi.py +8 -0
 
 
+def check_if(expr, info):
+    ifcond = expr.get('if')
+    if not ifcond or isinstance(ifcond, str):
+        return
+    if (not isinstance(ifcond, list) or
+        any([not isinstance(elt, str) for elt in ifcond])):
+        raise QAPISemError(info, "'if' condition requires a string or "
+                           "a list of string")

Wait a second! What's this list business? The commit message doesn't say.

Also, pep8 gripes:

scripts/qapi.py:647:9: E129 visually indented line with same indent as next logical line

scripts/qapi.py def check_keys(expr_elem, meta, required, optional=[]): · line 878

Quoted 10 lines
scripts/qapi.py +3 -0
+
+
 def check_type(info, source, value, allow_array=False,
                allow_dict=False, allow_optional=False,
                allow_metas=[]):
@@ -865,6 +875,7 @@ def check_keys(expr_elem, meta, required, optional=[]):
     expr = expr_elem['expr']
     info = expr_elem['info']
     name = expr[meta]
+    optional.append('if')

Caution!

    $ python
    Python 2.7.13 (default, May 10 2017, 20:04:36) 
Quoted 1 line
Quoted 1 line
Quoted 1 line

def surprise(arg=[]):

    ...     arg.append('if')
    ...     return arg
    ... 
    >>> surprise()
    ['if']
    >>> surprise()
    ['if', 'if']
    >>> surprise()
    ['if', 'if', 'if']

Never modify an argument that has list or dictionary default value. To avoid the temptation, never use such defaul values.

Quoted 13 lines

if not isinstance(name, str):
raise QAPISemError(info, "'%s' key must have a string value" % meta)
required = required + [meta]

scripts/qapi.py +2 -0
@@ -880,6 +891,8 @@ def check_keys(expr_elem, meta, required, optional=[]):
             raise QAPISemError(info,
                                "'%s' of %s '%s' should only use true value"
                                % (key, meta, name))
+        if key == 'if':
+            check_if(expr, info)
     for key in required:
         if key not in expr:
             raise QAPISemError(info, "Key '%s' is missing from %s '%s'"

scripts/qapi.py class QAPISchemaEntity(object): · line 1008

Quoted 7 lines
scripts/qapi.py +4 -0
@@ -989,6 +1002,10 @@ class QAPISchemaEntity(object):
         # such place).
         self.info = info
         self.doc = doc
+        self.ifcond = None
+
+    def set_ifcond(self, ifcond):
+        self.ifcond = ifcond

@ifcond is an awkward name, but I don't have better ideas.

Quoted 65 lines
scripts/qapi.py +11 -9
     def c_name(self):
         return c_name(self.name)
@@ -1017,26 +1034,26 @@ class QAPISchemaVisitor(object):
     def visit_builtin_type(self, name, info, json_type):
         pass
 
-    def visit_enum_type(self, name, info, values, prefix):
+    def visit_enum_type(self, name, info, values, prefix, ifcond):
         pass
 
-    def visit_array_type(self, name, info, element_type):
+    def visit_array_type(self, name, info, element_type, ifcond):
         pass
 
-    def visit_object_type(self, name, info, base, members, variants):
+    def visit_object_type(self, name, info, base, members, variants, ifcond):
         pass
 
-    def visit_object_type_flat(self, name, info, members, variants):
+    def visit_object_type_flat(self, name, info, members, variants, ifcond):
         pass
 
-    def visit_alternate_type(self, name, info, variants):
+    def visit_alternate_type(self, name, info, variants, ifcond):
         pass
 
     def visit_command(self, name, info, arg_type, ret_type,
-                      gen, success_response, boxed):
+                      gen, success_response, boxed, ifcond):
         pass
 
-    def visit_event(self, name, info, arg_type, boxed):
+    def visit_event(self, name, info, arg_type, boxed, ifcond):
         pass
 
 
@@ -1136,7 +1153,7 @@ class QAPISchemaEnumType(QAPISchemaType):
 
     def visit(self, visitor):
         visitor.visit_enum_type(self.name, self.info,
-                                self.member_names(), self.prefix)
+                                self.member_names(), self.prefix, self.ifcond)
 
 
 class QAPISchemaArrayType(QAPISchemaType):
@@ -1149,6 +1166,7 @@ class QAPISchemaArrayType(QAPISchemaType):
     def check(self, schema):
         self.element_type = schema.lookup_type(self._element_type_name)
         assert self.element_type
+        self.ifcond = self.element_type.ifcond
 
     def is_implicit(self):
         return True
@@ -1166,7 +1184,8 @@ class QAPISchemaArrayType(QAPISchemaType):
         return 'array of ' + elt_doc_type
 
     def visit(self, visitor):
-        visitor.visit_array_type(self.name, self.info, self.element_type)
+        visitor.visit_array_type(self.name, self.info, self.element_type,
+                                 self.ifcond)
 
 
 class QAPISchemaObjectType(QAPISchemaType):

scripts/qapi.py class QAPISchemaObjectType(QAPISchemaType): · line 1272

Quoted 9 lines
scripts/qapi.py +3 -2
@@ -1247,9 +1266,10 @@ class QAPISchemaObjectType(QAPISchemaType):
 
     def visit(self, visitor):
         visitor.visit_object_type(self.name, self.info,
-                                  self.base, self.local_members, self.variants)
+                                  self.base, self.local_members, self.variants,
+                                  self.ifcond)
         visitor.visit_object_type_flat(self.name, self.info,
-                                       self.members, self.variants)
+                                       self.members, self.variants, self.ifcond)

You break the line before self.ifcond almost everywhere, and when you don't, the line gets long. This one goes over the limit:

    scripts/qapi.py:1285:80: E501 line too long (80 > 79 characters)

Suggest to break it before self.ifcond everywhere.

Quoted 34 lines
scripts/qapi.py +6 -3
 class QAPISchemaMember(object):
@@ -1392,7 +1412,8 @@ class QAPISchemaAlternateType(QAPISchemaType):
         return 'value'
 
     def visit(self, visitor):
-        visitor.visit_alternate_type(self.name, self.info, self.variants)
+        visitor.visit_alternate_type(self.name, self.info,
+                                     self.variants, self.ifcond)
 
     def is_empty(self):
         return False
@@ -1434,7 +1455,8 @@ class QAPISchemaCommand(QAPISchemaEntity):
     def visit(self, visitor):
         visitor.visit_command(self.name, self.info,
                               self.arg_type, self.ret_type,
-                              self.gen, self.success_response, self.boxed)
+                              self.gen, self.success_response, self.boxed,
+                              self.ifcond)
 
 
 class QAPISchemaEvent(QAPISchemaEntity):
@@ -1462,7 +1484,8 @@ class QAPISchemaEvent(QAPISchemaEntity):
             raise QAPISemError(self.info, "Use of 'boxed' requires 'data'")
 
     def visit(self, visitor):
-        visitor.visit_event(self.name, self.info, self.arg_type, self.boxed)
+        visitor.visit_event(self.name, self.info, self.arg_type, self.boxed,
+                            self.ifcond)
 
 
 class QAPISchema(object):

scripts/qapi.py class QAPISchema(object): · line 1512

Quoted 10 lines
scripts/qapi.py +2 -1
@@ -1481,11 +1504,12 @@ class QAPISchema(object):
             print >>sys.stderr, err
             exit(1)
 
-    def _def_entity(self, ent):
+    def _def_entity(self, ent, ifcond=None):
         # Only the predefined types are allowed to not have info
         assert ent.info or self._predefining
         assert ent.name not in self._entity_dict
         self._entity_dict[ent.name] = ent
+        ent.set_ifcond(ifcond)

.set_ifcond(None) does the right thing.

However:

Quoted 5 lines
scripts/qapi.py +0 -0
     def lookup_entity(self, name, typ=None):
         ent = self._entity_dict.get(name)
@@ -1534,11 +1558,11 @@ class QAPISchema(object):
     def _make_enum_members(self, values):

scripts/qapi.py class QAPISchema(object): · line 1566

Quoted 10 lines
scripts/qapi.py +2 -2
         return [QAPISchemaMember(v) for v in values]
 
-    def _make_implicit_enum_type(self, name, info, values):
+    def _make_implicit_enum_type(self, name, info, values, ifcond):
         # See also QAPISchemaObjectTypeMember._pretty_owner()
         name = name + 'Kind'   # Use namespace reserved by add_name()
         self._def_entity(QAPISchemaEnumType(
-            name, info, None, self._make_enum_members(values), None))
+            name, info, None, self._make_enum_members(values), None), ifcond)
         return name

Why is ifcond not a constructor argument like name, info, and so forth? What makes it special?

Quoted 11 lines
scripts/qapi.py +2 -1
     def _make_array_type(self, element_type, info):
@@ -1547,22 +1571,26 @@ class QAPISchema(object):
             self._def_entity(QAPISchemaArrayType(name, info, element_type))
         return name
 
-    def _make_implicit_object_type(self, name, info, doc, role, members):
+    def _make_implicit_object_type(self, name, info, doc, role, members,
+                                   ifcond=None):
         if not members:
             return None

scripts/qapi.py class QAPISchema(object): · line 1585

Quoted 10 lines
scripts/qapi.py +4 -2
         # See also QAPISchemaObjectTypeMember._pretty_owner()
         name = 'q_obj_%s-%s' % (name, role)
-        if not self.lookup_entity(name, QAPISchemaObjectType):
+        if self.lookup_entity(name, QAPISchemaObjectType):
+            assert ifcond is None
+        else:
             self._def_entity(QAPISchemaObjectType(name, info, doc, None,
-                                                  members, None))
+                                                  members, None), ifcond)
         return name

Hmm, this smells like it might be the "variants objects types" mentioned in the commit message.

Types made with _make_implicit_object_type():

* The wrapper around "simple" union members, by _make_simple_variant()

* A flat union's base type when it's implicit, by _def_union_type()

* A command's argument type when it's implicit, by _def_command()

* An event's argument type when it's implicit, by _def_event()

Only the first one can be used more than once, namely when a type occurs in more than one simple union. The "correct" ifcond is the disjunction of all its user's ifconds. You make it use the *first* ifcond instead.
Wrong: breaks when one of the other simple unions has a condition that is true when the first one's is false.

Your commit message suggests you intended to make it unconditional instead. That would work: the worst that can happen is compiling a few q_obj_FOO_wrapper typedefs and visit_type_q_FOO_wrapper() functions that aren't actually used. Tolerable, in particular since I hope to get rid of "simple" unions some day.

Sadly, it would prevent us from making the visit functions for implicit types static, because unused static functions trigger warnings. Let's not worry about that now.

Generating the disjunction of all conditions wouldn't be terribly hard. I'm not asking for it, though.

You assert that implicit types are unconditional from the second use on. I guess you mean to assert the ones used more than once are unconditional:

typ = self.lookup_entity(name, QAPISchemaObjectType) if typ:
assert ifcond is None and typ.ifcond is None

But what you *should* assert is that the conditions are the same:

typ = self.lookup_entity(name, QAPISchemaObjectType) if typ:
assert ifcond == typ.ifcond

scripts/qapi.py class QAPISchema(object): · line 1593

Quoted 10 lines
scripts/qapi.py +3 -2
     def _def_enum_type(self, expr, info, doc):
         name = expr['enum']
         data = expr['data']
         prefix = expr.get('prefix')
-        self._def_entity(QAPISchemaEnumType(
-            name, info, doc, self._make_enum_members(data), prefix))
+        return self._def_entity(QAPISchemaEnumType(
+            name, info, doc, self._make_enum_members(data), prefix),
+                                expr.get('if'))

Covers enumeration types.

Why return? The (only) caller throws the value away...

scripts/qapi.py class QAPISchema(object): · line 1616

Quoted 10 lines
scripts/qapi.py +2 -1
     def _make_member(self, name, typ, info):
         optional = False
@@ -1584,7 +1612,8 @@ class QAPISchema(object):
         data = expr['data']
         self._def_entity(QAPISchemaObjectType(name, info, doc, base,
                                               self._make_members(data, info),
-                                              None))
+                                              None),
+                         expr.get('if'))

Covers struct types.

scripts/qapi.py class QAPISchema(object): · line 1629

Quoted 13 lines
scripts/qapi.py +3 -1
     def _make_variant(self, case, typ):
         return QAPISchemaObjectTypeVariant(case, typ)
@@ -1593,8 +1622,10 @@ class QAPISchema(object):
         if isinstance(typ, list):
             assert len(typ) == 1
             typ = self._make_array_type(typ[0], info)
+        type_entity = self.lookup_type(typ)
         typ = self._make_implicit_object_type(
-            typ, info, None, 'wrapper', [self._make_member('data', typ, info)])
+            typ, info, None, 'wrapper',
+            [self._make_member('data', typ, info)], type_entity.ifcond)
         return QAPISchemaObjectTypeVariant(case, typ)

A simple union member's wrapper type inherits its condition from the member type.

I think you need to pass None instead of type_entity.ifcond here.

scripts/qapi.py class QAPISchema(object): · line 1640

Quoted 11 lines
scripts/qapi.py +3 -2
     def _def_union_type(self, expr, info, doc):
@@ -1604,8 +1635,9 @@ class QAPISchema(object):
         tag_name = expr.get('discriminator')
         tag_member = None
         if isinstance(base, dict):
-            base = (self._make_implicit_object_type(
-                name, info, doc, 'base', self._make_members(base, info)))
+            base = self._make_implicit_object_type(
+                name, info, doc, 'base', self._make_members(base, info),
+                expr.get('if'))

A flat union's implicit base type inherits its condition from the flat union. Good.

scripts/qapi.py class QAPISchema(object): · line 1650

Quoted 10 lines
scripts/qapi.py +2 -1
         if tag_name:
             variants = [self._make_variant(key, value)
                         for (key, value) in data.iteritems()]
@@ -1614,14 +1646,16 @@ class QAPISchema(object):
             variants = [self._make_simple_variant(key, value, info)
                         for (key, value) in data.iteritems()]
             typ = self._make_implicit_enum_type(name, info,
-                                                [v.name for v in variants])
+                                                [v.name for v in variants],
+                                                expr.get('if'))

A flat union's implicit enumeration type inherits its condition from the flat union. Good.

scripts/qapi.py class QAPISchema(object): · line 1658

Quoted 9 lines
scripts/qapi.py +2 -1
             tag_member = QAPISchemaObjectTypeMember('type', typ, False)
             members = [tag_member]
         self._def_entity(
             QAPISchemaObjectType(name, info, doc, base, members,
                                  QAPISchemaObjectTypeVariants(tag_name,
                                                               tag_member,
-                                                              variants)))
+                                                              variants)),
+            expr.get('if'))

Covers union types.

Third use of expr.get('if') in this function. Please put it in a variable.

Actually, do that for *all* uses of expr[X] and expr.get(X) in class QAPISchema, because that's how the existing code works.

scripts/qapi.py class QAPISchema(object): · line 1671

Quoted 10 lines
scripts/qapi.py +2 -1
     def _def_alternate_type(self, expr, info, doc):
         name = expr['alternate']
@@ -1633,7 +1667,8 @@ class QAPISchema(object):
             QAPISchemaAlternateType(name, info, doc,
                                     QAPISchemaObjectTypeVariants(None,
                                                                  tag_member,
-                                                                 variants)))
+                                                                 variants)),
+            expr.get('if'))

Covers alternate types.

scripts/qapi.py class QAPISchema(object): · line 1683

Quoted 10 lines
scripts/qapi.py +2 -1
     def _def_command(self, expr, info, doc):
         name = expr['command']
@@ -1644,12 +1679,14 @@ class QAPISchema(object):
         boxed = expr.get('boxed', False)
         if isinstance(data, OrderedDict):
             data = self._make_implicit_object_type(
-                name, info, doc, 'arg', self._make_members(data, info))
+                name, info, doc, 'arg', self._make_members(data, info),
+                expr.get('if'))

A command's implicit argument type inherits its condition from the command. Good.

scripts/qapi.py class QAPISchema(object): · line 1689

Quoted 7 lines
scripts/qapi.py +2 -1
         if isinstance(rets, list):
             assert len(rets) == 1
             rets = self._make_array_type(rets[0], info)
         self._def_entity(QAPISchemaCommand(name, info, doc, data, rets,
-                                           gen, success_response, boxed))
+                                           gen, success_response, boxed),
+                         expr.get('if'))

Covers commands.

scripts/qapi.py class QAPISchema(object): · line 1698

Quoted 11 lines
scripts/qapi.py +2 -2
     def _def_event(self, expr, info, doc):
         name = expr['event']
@@ -1657,8 +1694,10 @@ class QAPISchema(object):
         boxed = expr.get('boxed', False)
         if isinstance(data, OrderedDict):
             data = self._make_implicit_object_type(
-                name, info, doc, 'arg', self._make_members(data, info))
-        self._def_entity(QAPISchemaEvent(name, info, doc, data, boxed))
+                name, info, doc, 'arg', self._make_members(data, info),
+                expr.get('if'))

An event's implicit argument type inherits its condition from the command. Good.

scripts/qapi.py class QAPISchema(object): · line 1700

Quoted 2 lines
scripts/qapi.py +2 -0
+        self._def_entity(QAPISchemaEvent(name, info, doc, data, boxed),
+                         expr.get('if'))

Covers events. You got them all. Good.

Quoted 7 lines
scripts/qapi.py +0 -0
     def _def_exprs(self):
         for expr_elem in self.exprs:
@@ -1848,6 +1887,54 @@ def guardend(name):
                  name=guardname(name))
 
 

scripts/qapi.py def guardend(name): · line 1899

Quoted 10 lines
scripts/qapi.py +10 -0
+def gen_if(ifcond, func=''):
+    if not ifcond:
+        return ''
+    if isinstance(ifcond, str):
+        ifcond = [ifcond]
+    ret = '\n'
+    for ifc in ifcond:
+        ret += mcgen('#if %(ifcond)s /* %(func)s */\n', ifcond=ifc, func=func)
+    ret += '\n'
+    return ret

Please use mcgen() like the existing code:

           ret += mcgen('''

#if %(ifcond)s /* %(func)s */
''', ifcond=ifc, func=func)

With the default value of @func, we get a useless, ugly comment /* */. If this can happen, please suppress the comment. Else, drop @func's default value.

Lists appear to be conjunctions. What for?

scripts/qapi.py def guardend(name): · line 1912

Quoted 13 lines
scripts/qapi.py +13 -0
+
+
+def gen_endif(ifcond, func=''):
+    if not ifcond:
+        return ''
+    if isinstance(ifcond, str):
+        ifcond = [ifcond]
+    ret = '\n'
+    for ifc in reversed(ifcond):
+        ret += mcgen('#endif /* %(ifcond)s %(func)s */\n',
+                     ifcond=ifc, func=func)
+    ret += '\n'
+    return ret

Likewise.

scripts/qapi.py def guardend(name): · line 1917

Quoted 5 lines
scripts/qapi.py +5 -0
+
+
+# wrap a method to add #if / #endif to generated code
+# self must have 'if_members' listing the attributes to wrap
+# the last argument of the wrapped function must be the 'ifcond'

Start your sentences with a capital letter, and end them with a period, please.

scripts/qapi.py def guardend(name): · line 1918

Quoted 1 line
scripts/qapi.py +1 -0
+def if_wrap(func):

Blank line, please.

scripts/qapi.py def guardend(name): · line 1931

Quoted 13 lines
scripts/qapi.py +13 -0
+    def func_wrapper(self, *args, **kwargs):
+        funcname = self.__class__.__name__ + '.' + func.__name__
+        ifcond = args[-1]
+        save = {}
+        for mem in self.if_members:
+            save[mem] = getattr(self, mem)
+        func(self, *args, **kwargs)
+        for mem, val in save.items():
+            newval = getattr(self, mem)
+            if newval != val:
+                assert newval.startswith(val)
+                newval = newval[len(val):]
+                val += gen_if(ifcond, funcname)

Emitting comments pointing to the QAPI schema into the generated code is often helpful. But this one points to QAPI generator code. Why is that useful?

scripts/qapi.py def guardend(name): · line 1937

Quoted 6 lines
scripts/qapi.py +6 -0
+                val += newval
+                val += gen_endif(ifcond, funcname)
+            setattr(self, mem, val)
+
+    return func_wrapper
+

pep8 again:

    scripts/qapi.py:1955:1: E302 expected 2 blank lines, found 1

Peeking ahead: this function is used as a decorator. Let's help the reader and mention that in the function comment, or by naming the function suitably. ifcond_decorator?

Messing with the wrapped method's class's attributes is naughty. Worse, it's hard to understand. What alternatives have you considered?

Quoted 62 lines
scripts/qapi.py +0 -0
 def gen_enum_lookup(name, values, prefix=None):
     ret = mcgen('''
 
scripts/qapi-commands.py +3 -1
index 974d0a4a80..19b1bb9b88 100644
@@ -228,6 +228,7 @@ class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
         self.defn = None
         self._regy = None
         self._visited_ret_types = None
+        self.if_members = ['decl', 'defn', '_regy']
 
     def visit_begin(self, schema):
         self.decl = ''
@@ -240,8 +241,9 @@ class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
         self._regy = None
         self._visited_ret_types = None
 
+    @if_wrap
     def visit_command(self, name, info, arg_type, ret_type,
-                      gen, success_response, boxed):
+                      gen, success_response, boxed, ifcond):
         if not gen:
             return
         self.decl += gen_command_decl(name, arg_type, boxed, ret_type)
scripts/qapi-event.py +3 -1
index bcbef1035f..cad9ece790 100644
@@ -152,6 +152,7 @@ class QAPISchemaGenEventVisitor(QAPISchemaVisitor):
         self.decl = None
         self.defn = None
         self._event_names = None
+        self.if_members = ['decl', 'defn']
 
     def visit_begin(self, schema):
         self.decl = ''
@@ -163,7 +164,8 @@ class QAPISchemaGenEventVisitor(QAPISchemaVisitor):
         self.defn += gen_enum_lookup(event_enum_name, self._event_names)
         self._event_names = None
 
-    def visit_event(self, name, info, arg_type, boxed):
+    @if_wrap
+    def visit_event(self, name, info, arg_type, boxed, ifcond):
         self.decl += gen_event_send_decl(name, arg_type, boxed)
         self.defn += gen_event_send(name, arg_type, boxed)
         self._event_names.append(name)
scripts/qapi-introspect.py +1 -1
index fc72cdb66d..ecfb0f2752 100644
@@ -12,7 +12,7 @@
 from qapi import *
 
 
-def to_qlit(obj, level=0, first_indent=True):
+def to_qlit(obj, level=0, first_indent=True, suffix=''):
     def indent(level):
         return level * 4 * ' '
     ret = ''

scripts/qapi-introspect.py def to_qlit(obj, level=0, first_indent=True): · line 26

Quoted 7 lines
scripts/qapi-introspect.py +4 -0
@@ -20,14 +20,20 @@ def to_qlit(obj, level=0, first_indent=True):
         ret += indent(level)
     if obj is None:
         ret += 'QLIT_QNULL'
+    elif isinstance(obj, tuple):
+        obj, ifcond =  obj
+        ret += gen_if(ifcond)
+        ret += to_qlit(obj, level, False) + suffix

Keyword argument instead of bare False, please.

scripts/qapi-introspect.py def to_qlit(obj, level=0, first_indent=True): · line 28

Quoted 2 lines
scripts/qapi-introspect.py +2 -0
+        ret += gen_endif(ifcond)
+        suffix = ''

New case tuple, for generating conditionals. Okay.

scripts/qapi-introspect.py def to_qlit(obj, level=0, first_indent=True): · line 32

Quoted 5 lines
scripts/qapi-introspect.py +1 -1
     elif isinstance(obj, str):
         ret += 'QLIT_QSTR(' + '"' + obj.replace('"', r'\"') + '"' + ')'
     elif isinstance(obj, list):
-        elts = [to_qlit(elt, level + 1)
+        elts = [to_qlit(elt, level + 1, True, ",")

Make that

           elts = [to_qlit(elt, level + 1, suffix=",")
Quoted 9 lines
scripts/qapi-introspect.py +1 -1
                 for elt in obj]
         elts.append(indent(level + 1) + "{ }")
         ret += 'QLIT_QLIST(((QLitObject[]) {\n'
-        ret += ',\n'.join(elts) + '\n'
+        ret += '\n'.join(elts) + '\n'
         ret += indent(level) + '}))'
     elif isinstance(obj, dict):
         elts = [ indent(level + 1) + '{ "%s", %s }' %

scripts/qapi-introspect.py def to_qlit(obj, level=0, first_indent=True): · line 48

Quoted 5 lines
scripts/qapi-introspect.py +1 -1
@@ -39,7 +45,7 @@ def to_qlit(obj, level=0, first_indent=True):
         ret += indent(level) + '}))'
     else:
         assert False                # not implemented
-    return ret
+    return ret + suffix

This is getting really hard to review --- my brain is about to overflow and shut down for the day. Can you split off some preparatory work? The introduction of suffix here, perhaps?

Quoted 62 lines
scripts/qapi-introspect.py +14 -13
 class QAPISchemaGenIntrospectVisitor(QAPISchemaVisitor):
@@ -113,12 +119,12 @@ const QLitObject %(c_name)s = %(c_string)s;
             return '[' + self._use_type(typ.element_type) + ']'
         return self._name(typ.name)
 
-    def _gen_qlit(self, name, mtype, obj):
+    def _gen_qlit(self, name, mtype, obj, ifcond):
         if mtype not in ('command', 'event', 'builtin', 'array'):
             name = self._name(name)
         obj['name'] = name
         obj['meta-type'] = mtype
-        self._qlits.append(obj)
+        self._qlits.append((obj, ifcond))
 
     def _gen_member(self, member):
         ret = {'name': member.name, 'type': self._use_type(member.type)}
@@ -134,38 +140,40 @@ const QLitObject %(c_name)s = %(c_string)s;
         return {'case': variant.name, 'type': self._use_type(variant.type)}
 
     def visit_builtin_type(self, name, info, json_type):
-        self._gen_qlit(name, 'builtin', {'json-type': json_type})
+        self._gen_qlit(name, 'builtin', {'json-type': json_type}, None)
 
-    def visit_enum_type(self, name, info, values, prefix):
-        self._gen_qlit(name, 'enum', {'values': values})
+    def visit_enum_type(self, name, info, values, prefix, ifcond):
+        self._gen_qlit(name, 'enum', {'values': values}, ifcond)
 
-    def visit_array_type(self, name, info, element_type):
+    def visit_array_type(self, name, info, element_type, ifcond):
         element = self._use_type(element_type)
-        self._gen_qlit('[' + element + ']', 'array', {'element-type': element})
+        self._gen_qlit('[' + element + ']', 'array', {'element-type': element},
+                       ifcond)
 
-    def visit_object_type_flat(self, name, info, members, variants):
+    def visit_object_type_flat(self, name, info, members, variants, ifcond):
         obj = {'members': [self._gen_member(m) for m in members]}
         if variants:
             obj.update(self._gen_variants(variants.tag_member.name,
                                           variants.variants))
-        self._gen_qlit(name, 'object', obj)
+        self._gen_qlit(name, 'object', obj, ifcond)
 
-    def visit_alternate_type(self, name, info, variants):
+    def visit_alternate_type(self, name, info, variants, ifcond):
         self._gen_qlit(name, 'alternate',
                        {'members': [{'type': self._use_type(m.type)}
-                                    for m in variants.variants]})
+                                    for m in variants.variants]}, ifcond)
 
     def visit_command(self, name, info, arg_type, ret_type,
-                      gen, success_response, boxed):
+                      gen, success_response, boxed, ifcond):
         arg_type = arg_type or self._schema.the_empty_object_type
         ret_type = ret_type or self._schema.the_empty_object_type
         self._gen_qlit(name, 'command',
                        {'arg-type': self._use_type(arg_type),
-                        'ret-type': self._use_type(ret_type)})
+                        'ret-type': self._use_type(ret_type)}, ifcond)

scripts/qapi-introspect.py const QLitObject %(c_name)s = %(c_string)s; · line 179

Quoted 10 lines
scripts/qapi-introspect.py +3 -2
 
-    def visit_event(self, name, info, arg_type, boxed):
+    def visit_event(self, name, info, arg_type, boxed, ifcond):
         arg_type = arg_type or self._schema.the_empty_object_type
-        self._gen_qlit(name, 'event', {'arg-type': self._use_type(arg_type)})
+        self._gen_qlit(name, 'event', {'arg-type': self._use_type(arg_type)},
+                       ifcond)
 
 # Debugging aid: unmask QAPI schema's type names
 # We normally mask them, because they're not QMP wire ABI

Out of review brainpower for today. Hope to resume tomorrow.

[...]

#20 lore raw

Re: [Qemu-devel] [PATCH 07/26] qapi: add 'if' condition on top-level schema elements

Markus Armbruster <[email protected]> writes:

[...]

Quoted 3 lines

Out of review brainpower for today. Hope to resume tomorrow.

[...]

Nope, I'm giving up on this one. Please split it for reviewability. Suggested split:

1. Preparatory refactoring for step 2, step by step

2. Frontend part: accept and check 'if', step by step

The accepted conditions should be visible in
tests/qapi-schema/qapi-schema-test.out.

3. Preparatory refactoring for step 4, step by step

4. Backend part: generate the ifdeffery, step by step

You already split off qapi2texi steps [PATCH 12-15]. Good. Perhaps that's all that can be split off, perhaps not.

PATCH 08-11 may well profit from the same treatment.

Moving on to PATCH 16.

#21 lore raw

Re: [Qemu-devel] [PATCH 07/26] qapi: add 'if' condition on top-level schema elements

In reply to Markus Armbruster

Hi

Quoted 8 lines · Markus Armbruster
Quoted 2 lines · Marc-André Lureau

Add 'if' c-preprocessor condition on top-level schema elements:
struct, enum, union, alternate, command, event.

An example would be useful here. Your cover letter has nice ones, would be a shame not to preserve them for posterity in the commit log.

ok

Quoted 4 lines
Quoted 1 line

Variants objects types are created outside of #if blocks, since they

What are "variants objects types"?

I think I meant implicit objects types, that may be created by variant.

Quoted 44 lines
Quoted 4 lines

may be shared by various types. Even if unused, this shouldn't be an issue, since those are internal types.

Note that there is no checks in qapi scripts to verify that elements

there are

Quoted 3 lines

have compatible conditions (ex: if-struct used by a if-foo command). This may thus fail at C build time if they don't share the same subset of conditions.

Fair enough.

Quoted 31 lines
---
 scripts/qapi.py                         | 153 +++++++++++++++++++++++++-------
 scripts/qapi-commands.py                |   4 +-
 scripts/qapi-event.py                   |   4 +-
 scripts/qapi-introspect.py              |  46 ++++++----
 scripts/qapi-types.py                   |  51 +++++++----
 scripts/qapi-visit.py                   |  13 ++-
 scripts/qapi2texi.py                    |  10 +--
 tests/Makefile.include                  |   1 +
 tests/qapi-schema/bad-if.err            |   1 +
 tests/qapi-schema/bad-if.exit           |   1 +
 tests/qapi-schema/bad-if.json           |   3 +
 tests/qapi-schema/bad-if.out            |   0
 tests/qapi-schema/qapi-schema-test.json |  20 +++++
 tests/qapi-schema/qapi-schema-test.out  |  31 +++++++
 tests/qapi-schema/test-qapi.py          |  21 +++--
 15 files changed, 272 insertions(+), 87 deletions(-)
 create mode 100644 tests/qapi-schema/bad-if.err
 create mode 100644 tests/qapi-schema/bad-if.exit
 create mode 100644 tests/qapi-schema/bad-if.json
 create mode 100644 tests/qapi-schema/bad-if.out
scripts/qapi.py +0 -0
index 4ecc19e944..79ba1e93da 100644
@@ -639,6 +639,16 @@ def add_name(name, info, meta, implicit=False):
     all_names[name] = meta
 
 
Quoted 11 lines

scripts/qapi.py def add_name(name, info, meta, implicit=False): · line 649

Quoted 8 lines
scripts/qapi.py +8 -0
+def check_if(expr, info):
+    ifcond = expr.get('if')
+    if not ifcond or isinstance(ifcond, str):
+        return
+    if (not isinstance(ifcond, list) or
+        any([not isinstance(elt, str) for elt in ifcond])):
+        raise QAPISemError(info, "'if' condition requires a string or "
+                           "a list of string")

Wait a second! What's this list business? The commit message doesn't say.

Updated commit message, and documented in docs/devel/qapi-code-gen.txt

Quoted 5 lines

Also, pep8 gripes:

scripts/qapi.py:647:9: E129 visually indented line with same indent as next logical line

fixed

Quoted 26 lines

scripts/qapi.py def check_keys(expr_elem, meta, required, optional=[]): · line 878

Quoted 10 lines
scripts/qapi.py +3 -0
+
+
 def check_type(info, source, value, allow_array=False,
                allow_dict=False, allow_optional=False,
                allow_metas=[]):
@@ -865,6 +875,7 @@ def check_keys(expr_elem, meta, required, optional=[]):
     expr = expr_elem['expr']
     info = expr_elem['info']
     name = expr[meta]
+    optional.append('if')

Caution!

    $ python
    Python 2.7.13 (default, May 10 2017, 20:04:36)
Quoted 1 line
Quoted 1 line
Quoted 1 line

def surprise(arg=[]):

    ...     arg.append('if')
    ...     return arg
    ...
    >>> surprise()
    ['if']
    >>> surprise()
    ['if', 'if']
    >>> surprise()
    ['if', 'if', 'if']
Quoted 2 lines

Never modify an argument that has list or dictionary default value. To avoid the temptation, never use such defaul values.

Oops! Without bad consequences here, but fixed anyway.

Quoted 15 lines
Quoted 15 lines

if not isinstance(name, str):
raise QAPISemError(info, "'%s' key must have a string value" % meta)
required = required + [meta]

scripts/qapi.py +2 -0
@@ -880,6 +891,8 @@ def check_keys(expr_elem, meta, required, optional=[]):
             raise QAPISemError(info,
                                "'%s' of %s '%s' should only use true value"
                                % (key, meta, name))
+        if key == 'if':
+            check_if(expr, info)
     for key in required:
         if key not in expr:
             raise QAPISemError(info, "Key '%s' is missing from %s '%s'"
@@ -989,6 +1002,10 @@ class QAPISchemaEntity(object):
         # such place).
Quoted 8 lines

scripts/qapi.py class QAPISchemaEntity(object): · line 1008

Quoted 6 lines
scripts/qapi.py +4 -0
         self.info = info
         self.doc = doc
+        self.ifcond = None
+
+    def set_ifcond(self, ifcond):
+        self.ifcond = ifcond

@ifcond is an awkward name, but I don't have better ideas.

Yeah, I got used to it now ;)

Quoted 79 lines
Quoted 66 lines
scripts/qapi.py +11 -9
     def c_name(self):
         return c_name(self.name)
@@ -1017,26 +1034,26 @@ class QAPISchemaVisitor(object):
     def visit_builtin_type(self, name, info, json_type):
         pass
 
-    def visit_enum_type(self, name, info, values, prefix):
+    def visit_enum_type(self, name, info, values, prefix, ifcond):
         pass
 
-    def visit_array_type(self, name, info, element_type):
+    def visit_array_type(self, name, info, element_type, ifcond):
         pass
 
-    def visit_object_type(self, name, info, base, members, variants):
+    def visit_object_type(self, name, info, base, members, variants, ifcond):
         pass
 
-    def visit_object_type_flat(self, name, info, members, variants):
+    def visit_object_type_flat(self, name, info, members, variants, ifcond):
         pass
 
-    def visit_alternate_type(self, name, info, variants):
+    def visit_alternate_type(self, name, info, variants, ifcond):
         pass
 
     def visit_command(self, name, info, arg_type, ret_type,
-                      gen, success_response, boxed):
+                      gen, success_response, boxed, ifcond):
         pass
 
-    def visit_event(self, name, info, arg_type, boxed):
+    def visit_event(self, name, info, arg_type, boxed, ifcond):
         pass
 
 
@@ -1136,7 +1153,7 @@ class QAPISchemaEnumType(QAPISchemaType):
 
     def visit(self, visitor):
         visitor.visit_enum_type(self.name, self.info,
-                                self.member_names(), self.prefix)
+                                self.member_names(), self.prefix, self.ifcond)
 
 
 class QAPISchemaArrayType(QAPISchemaType):
@@ -1149,6 +1166,7 @@ class QAPISchemaArrayType(QAPISchemaType):
     def check(self, schema):
         self.element_type = schema.lookup_type(self._element_type_name)
         assert self.element_type
+        self.ifcond = self.element_type.ifcond
 
     def is_implicit(self):
         return True
@@ -1166,7 +1184,8 @@ class QAPISchemaArrayType(QAPISchemaType):
         return 'array of ' + elt_doc_type
 
     def visit(self, visitor):
-        visitor.visit_array_type(self.name, self.info, self.element_type)
+        visitor.visit_array_type(self.name, self.info, self.element_type,
+                                 self.ifcond)
 
 
 class QAPISchemaObjectType(QAPISchemaType):

scripts/qapi.py class QAPISchemaObjectType(QAPISchemaType): · line 1272

Quoted 9 lines
scripts/qapi.py +3 -2
@@ -1247,9 +1266,10 @@ class QAPISchemaObjectType(QAPISchemaType):
 
     def visit(self, visitor):
         visitor.visit_object_type(self.name, self.info,
-                                  self.base, self.local_members, self.variants)
+                                  self.base, self.local_members, self.variants,
+                                  self.ifcond)
         visitor.visit_object_type_flat(self.name, self.info,
-                                       self.members, self.variants)
+                                       self.members, self.variants, self.ifcond)

You break the line before self.ifcond almost everywhere, and when you don't, the line gets long. This one goes over the limit:

Quoted 4 lines
    scripts/qapi.py:1285:80: E501 line too long (80 > 79 characters)

Suggest to break it before self.ifcond everywhere.

ok

Quoted 56 lines
Quoted 34 lines
scripts/qapi.py +6 -3
 class QAPISchemaMember(object):
@@ -1392,7 +1412,8 @@ class QAPISchemaAlternateType(QAPISchemaType):
         return 'value'
 
     def visit(self, visitor):
-        visitor.visit_alternate_type(self.name, self.info, self.variants)
+        visitor.visit_alternate_type(self.name, self.info,
+                                     self.variants, self.ifcond)
 
     def is_empty(self):
         return False
@@ -1434,7 +1455,8 @@ class QAPISchemaCommand(QAPISchemaEntity):
     def visit(self, visitor):
         visitor.visit_command(self.name, self.info,
                               self.arg_type, self.ret_type,
-                              self.gen, self.success_response, self.boxed)
+                              self.gen, self.success_response, self.boxed,
+                              self.ifcond)
 
 
 class QAPISchemaEvent(QAPISchemaEntity):
@@ -1462,7 +1484,8 @@ class QAPISchemaEvent(QAPISchemaEntity):
             raise QAPISemError(self.info, "Use of 'boxed' requires 'data'")
 
     def visit(self, visitor):
-        visitor.visit_event(self.name, self.info, self.arg_type, self.boxed)
+        visitor.visit_event(self.name, self.info, self.arg_type, self.boxed,
+                            self.ifcond)
 
 
 class QAPISchema(object):

scripts/qapi.py class QAPISchema(object): · line 1512

Quoted 10 lines
scripts/qapi.py +2 -1
@@ -1481,11 +1504,12 @@ class QAPISchema(object):
             print >>sys.stderr, err
             exit(1)
 
-    def _def_entity(self, ent):
+    def _def_entity(self, ent, ifcond=None):
         # Only the predefined types are allowed to not have info
         assert ent.info or self._predefining
         assert ent.name not in self._entity_dict
         self._entity_dict[ent.name] = ent
+        ent.set_ifcond(ifcond)

.set_ifcond(None) does the right thing.

However:

Quoted 7 lines
scripts/qapi.py +0 -0
     def lookup_entity(self, name, typ=None):
         ent = self._entity_dict.get(name)
@@ -1534,11 +1558,11 @@ class QAPISchema(object):
     def _make_enum_members(self, values):
         return [QAPISchemaMember(v) for v in values]
 
Quoted 12 lines

scripts/qapi.py class QAPISchema(object): · line 1566

Quoted 8 lines
scripts/qapi.py +2 -2
-    def _make_implicit_enum_type(self, name, info, values):
+    def _make_implicit_enum_type(self, name, info, values, ifcond):
         # See also QAPISchemaObjectTypeMember._pretty_owner()
         name = name + 'Kind'   # Use namespace reserved by add_name()
         self._def_entity(QAPISchemaEnumType(
-            name, info, None, self._make_enum_members(values), None))
+            name, info, None, self._make_enum_members(values), None), ifcond)
         return name

Why is ifcond not a constructor argument like name, info, and so forth? What makes it special?

I think it was easier that way (builder pattern), but it make sense as constructor too. Changed

Quoted 47 lines
Quoted 11 lines
scripts/qapi.py +2 -1
     def _make_array_type(self, element_type, info):
@@ -1547,22 +1571,26 @@ class QAPISchema(object):
             self._def_entity(QAPISchemaArrayType(name, info, element_type))
         return name
 
-    def _make_implicit_object_type(self, name, info, doc, role, members):
+    def _make_implicit_object_type(self, name, info, doc, role, members,
+                                   ifcond=None):
         if not members:
             return None

scripts/qapi.py class QAPISchema(object): · line 1585

Quoted 10 lines
scripts/qapi.py +4 -2
         # See also QAPISchemaObjectTypeMember._pretty_owner()
         name = 'q_obj_%s-%s' % (name, role)
-        if not self.lookup_entity(name, QAPISchemaObjectType):
+        if self.lookup_entity(name, QAPISchemaObjectType):
+            assert ifcond is None
+        else:
             self._def_entity(QAPISchemaObjectType(name, info, doc, None,
-                                                  members, None))
+                                                  members, None), ifcond)
         return name

Hmm, this smells like it might be the "variants objects types" mentioned in the commit message.

Types made with _make_implicit_object_type():

* The wrapper around "simple" union members, by _make_simple_variant()

* A flat union's base type when it's implicit, by _def_union_type()

* A command's argument type when it's implicit, by _def_command()

* An event's argument type when it's implicit, by _def_event()

Only the first one can be used more than once, namely when a type occurs in more than one simple union. The "correct" ifcond is the disjunction of all its user's ifconds. You make it use the *first* ifcond instead.
Wrong: breaks when one of the other simple unions has a condition that is true when the first one's is false.

Your commit message suggests you intended to make it unconditional instead. That would work: the worst that can happen is compiling a few q_obj_FOO_wrapper typedefs and visit_type_q_FOO_wrapper() functions that aren't actually used. Tolerable, in particular since I hope to get rid of "simple" unions some day.

Quoted 6 lines

Sadly, it would prevent us from making the visit functions for implicit types static, because unused static functions trigger warnings. Let's not worry about that now.

Generating the disjunction of all conditions wouldn't be terribly hard. I'm not asking for it, though.

I suggest to tackle it as follow-up then. Added a FIXME

Quoted 9 lines

You assert that implicit types are unconditional from the second use on. I guess you mean to assert the ones used more than once are unconditional:

typ = self.lookup_entity(name, QAPISchemaObjectType) if typ:
assert ifcond is None and typ.ifcond is None

Quoted 7 lines

But what you *should* assert is that the conditions are the same:

typ = self.lookup_entity(name, QAPISchemaObjectType) if typ:
assert ifcond == typ.ifcond

Quoted 1 line

ok

Quoted 10 lines

scripts/qapi.py class QAPISchema(object): · line 1593

Quoted 9 lines

def _def_enum_type(self, expr, info, doc):
name = expr['enum']
data = expr['data']
prefix = expr.get('prefix')

scripts/qapi.py +3 -2
-        self._def_entity(QAPISchemaEnumType(
-            name, info, doc, self._make_enum_members(data), prefix))
+        return self._def_entity(QAPISchemaEnumType(
+            name, info, doc, self._make_enum_members(data), prefix),
+                                expr.get('if'))
Quoted 3 lines

Covers enumeration types.

Why return? The (only) caller throws the value away...

left-over, fixed

Quoted 31 lines

scripts/qapi.py class QAPISchema(object): · line 1616

Quoted 11 lines

def _make_member(self, name, typ, info):
optional = False

scripts/qapi.py +2 -1
@@ -1584,7 +1612,8 @@ class QAPISchema(object):
         data = expr['data']
         self._def_entity(QAPISchemaObjectType(name, info, doc, base,
                                               self._make_members(data, info),
-                                              None))
+                                              None),
+                         expr.get('if'))

Covers struct types.

scripts/qapi.py class QAPISchema(object): · line 1629

Quoted 13 lines
scripts/qapi.py +3 -1
     def _make_variant(self, case, typ):
         return QAPISchemaObjectTypeVariant(case, typ)
@@ -1593,8 +1622,10 @@ class QAPISchema(object):
         if isinstance(typ, list):
             assert len(typ) == 1
             typ = self._make_array_type(typ[0], info)
+        type_entity = self.lookup_type(typ)
         typ = self._make_implicit_object_type(
-            typ, info, None, 'wrapper', [self._make_member('data', typ, info)])
+            typ, info, None, 'wrapper',
+            [self._make_member('data', typ, info)], type_entity.ifcond)
         return QAPISchemaObjectTypeVariant(case, typ)

A simple union member's wrapper type inherits its condition from the member type.

Quoted 1 line

I think you need to pass None instead of type_entity.ifcond here.

That doesn't work, visitor symbols are missing in generated code. Why shouldn't the wrapper share the same condition as the underlying type?

Quoted 40 lines

scripts/qapi.py class QAPISchema(object): · line 1640

Quoted 12 lines
scripts/qapi.py +3 -2
     def _def_union_type(self, expr, info, doc):
@@ -1604,8 +1635,9 @@ class QAPISchema(object):
         tag_name = expr.get('discriminator')
         tag_member = None
         if isinstance(base, dict):
-            base = (self._make_implicit_object_type(
-                name, info, doc, 'base', self._make_members(base, info)))
+            base = self._make_implicit_object_type(
+                name, info, doc, 'base', self._make_members(base, info),
+                expr.get('if'))

A flat union's implicit base type inherits its condition from the flat union. Good.

scripts/qapi.py class QAPISchema(object): · line 1650

Quoted 10 lines
scripts/qapi.py +2 -1
         if tag_name:
             variants = [self._make_variant(key, value)
                         for (key, value) in data.iteritems()]
@@ -1614,14 +1646,16 @@ class QAPISchema(object):
             variants = [self._make_simple_variant(key, value, info)
                         for (key, value) in data.iteritems()]
             typ = self._make_implicit_enum_type(name, info,
-                                                [v.name for v in variants])
+                                                [v.name for v in variants],
+                                                expr.get('if'))

A flat union's implicit enumeration type inherits its condition from the flat union. Good.

scripts/qapi.py class QAPISchema(object): · line 1658

Quoted 9 lines
scripts/qapi.py +2 -1
             tag_member = QAPISchemaObjectTypeMember('type', typ, False)
             members = [tag_member]
         self._def_entity(
             QAPISchemaObjectType(name, info, doc, base, members,
                                  QAPISchemaObjectTypeVariants(tag_name,
                                                               tag_member,
-                                                              variants)))
+                                                              variants)),
+            expr.get('if'))
Quoted 7 lines

Covers union types.

Third use of expr.get('if') in this function. Please put it in a variable.

Actually, do that for *all* uses of expr[X] and expr.get(X) in class QAPISchema, because that's how the existing code works.

done

Quoted 65 lines

scripts/qapi.py class QAPISchema(object): · line 1671

Quoted 11 lines
scripts/qapi.py +2 -1
     def _def_alternate_type(self, expr, info, doc):
         name = expr['alternate']
@@ -1633,7 +1667,8 @@ class QAPISchema(object):
             QAPISchemaAlternateType(name, info, doc,
                                     QAPISchemaObjectTypeVariants(None,
                                                                  tag_member,
-                                                                 variants)))
+                                                                 variants)),
+            expr.get('if'))

Covers alternate types.

scripts/qapi.py class QAPISchema(object): · line 1683

Quoted 10 lines
scripts/qapi.py +2 -1
     def _def_command(self, expr, info, doc):
         name = expr['command']
@@ -1644,12 +1679,14 @@ class QAPISchema(object):
         boxed = expr.get('boxed', False)
         if isinstance(data, OrderedDict):
             data = self._make_implicit_object_type(
-                name, info, doc, 'arg', self._make_members(data, info))
+                name, info, doc, 'arg', self._make_members(data, info),
+                expr.get('if'))

A command's implicit argument type inherits its condition from the command. Good.

scripts/qapi.py class QAPISchema(object): · line 1689

Quoted 7 lines
scripts/qapi.py +2 -1
         if isinstance(rets, list):
             assert len(rets) == 1
             rets = self._make_array_type(rets[0], info)
         self._def_entity(QAPISchemaCommand(name, info, doc, data, rets,
-                                           gen, success_response, boxed))
+                                           gen, success_response, boxed),
+                         expr.get('if'))

Covers commands.

scripts/qapi.py class QAPISchema(object): · line 1698

Quoted 11 lines
scripts/qapi.py +2 -2
     def _def_event(self, expr, info, doc):
         name = expr['event']
@@ -1657,8 +1694,10 @@ class QAPISchema(object):
         boxed = expr.get('boxed', False)
         if isinstance(data, OrderedDict):
             data = self._make_implicit_object_type(
-                name, info, doc, 'arg', self._make_members(data, info))
-        self._def_entity(QAPISchemaEvent(name, info, doc, data, boxed))
+                name, info, doc, 'arg', self._make_members(data, info),
+                expr.get('if'))

An event's implicit argument type inherits its condition from the command. Good.

scripts/qapi.py class QAPISchema(object): · line 1700

Quoted 2 lines
scripts/qapi.py +2 -0
+        self._def_entity(QAPISchemaEvent(name, info, doc, data, boxed),
+                         expr.get('if'))

Covers events. You got them all. Good.

Quoted 7 lines
scripts/qapi.py +0 -0
     def _def_exprs(self):
         for expr_elem in self.exprs:
@@ -1848,6 +1887,54 @@ def guardend(name):
                  name=guardname(name))
 
 
Quoted 13 lines

scripts/qapi.py def guardend(name): · line 1899

Quoted 10 lines
scripts/qapi.py +10 -0
+def gen_if(ifcond, func=''):
+    if not ifcond:
+        return ''
+    if isinstance(ifcond, str):
+        ifcond = [ifcond]
+    ret = '\n'
+    for ifc in ifcond:
+        ret += mcgen('#if %(ifcond)s /* %(func)s */\n', ifcond=ifc, func=func)
+    ret += '\n'
+    return ret

Please use mcgen() like the existing code:

mcgen() does indent and fails with pre-processor lines. I added a comment

Quoted 9 lines
           ret += mcgen('''
   #if %(ifcond)s /* %(func)s */
   ''', ifcond=ifc, func=func)

With the default value of @func, we get a useless, ugly comment /* */. If this can happen, please suppress the comment. Else, drop @func's default value.

Lists appear to be conjunctions. What for?

I added some doc in qapi-code-gen.txt

Quoted 17 lines
Quoted 4 lines
scripts/qapi.py +3 -0
+
+
+def gen_endif(ifcond, func=''):

scripts/qapi.py def guardend(name): · line 1912

Quoted 10 lines
scripts/qapi.py +10 -0
+    if not ifcond:
+        return ''
+    if isinstance(ifcond, str):
+        ifcond = [ifcond]
+    ret = '\n'
+    for ifc in reversed(ifcond):
+        ret += mcgen('#endif /* %(ifcond)s %(func)s */\n',
+                     ifcond=ifc, func=func)
+    ret += '\n'
+    return ret

Likewise.

Quoted 8 lines

scripts/qapi.py def guardend(name): · line 1917

Quoted 5 lines
scripts/qapi.py +5 -0
+
+
+# wrap a method to add #if / #endif to generated code
+# self must have 'if_members' listing the attributes to wrap
+# the last argument of the wrapped function must be the 'ifcond'

Start your sentences with a capital letter, and end them with a period, please.

ok

Quoted 4 lines

scripts/qapi.py def guardend(name): · line 1918

Quoted 2 lines
scripts/qapi.py +1 -0
+def if_wrap(func):

Blank line, please.

ok

Quoted 15 lines
Quoted 4 lines
scripts/qapi.py +3 -0
+    def func_wrapper(self, *args, **kwargs):
+        funcname = self.__class__.__name__ + '.' + func.__name__
+        ifcond = args[-1]

scripts/qapi.py def guardend(name): · line 1931

Quoted 10 lines
scripts/qapi.py +10 -0
+        save = {}
+        for mem in self.if_members:
+            save[mem] = getattr(self, mem)
+        func(self, *args, **kwargs)
+        for mem, val in save.items():
+            newval = getattr(self, mem)
+            if newval != val:
+                assert newval.startswith(val)
+                newval = newval[len(val):]
+                val += gen_if(ifcond, funcname)
Quoted 3 lines

Emitting comments pointing to the QAPI schema into the generated code is often helpful. But this one points to QAPI generator code. Why is that useful?

That was mostly useful during development, removed

Quoted 10 lines

scripts/qapi.py def guardend(name): · line 1937

Quoted 7 lines
scripts/qapi.py +6 -0
+                val += newval
+                val += gen_endif(ifcond, funcname)
+            setattr(self, mem, val)
+
+    return func_wrapper
+

pep8 again:

Quoted 5 lines
    scripts/qapi.py:1955:1: E302 expected 2 blank lines, found 1

Peeking ahead: this function is used as a decorator. Let's help the reader and mention that in the function comment, or by naming the function suitably. ifcond_decorator?

done

Quoted 3 lines

Messing with the wrapped method's class's attributes is naughty. Worse, it's hard to understand. What alternatives have you considered?

Well, I started writing the code that checked if members got code added, I had to put some enter()/leave() code everywhere. Then I realize this could easily be the job for a decorator. I think the end result is pretty neat. If you have a better idea, can you do it in a follow-up?

Quoted 57 lines
Quoted 57 lines
scripts/qapi.py +0 -0
 def gen_enum_lookup(name, values, prefix=None):
     ret = mcgen('''
 
scripts/qapi-commands.py +3 -1
index 974d0a4a80..19b1bb9b88 100644
@@ -228,6 +228,7 @@ class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
         self.defn = None
         self._regy = None
         self._visited_ret_types = None
+        self.if_members = ['decl', 'defn', '_regy']
 
     def visit_begin(self, schema):
         self.decl = ''
@@ -240,8 +241,9 @@ class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
         self._regy = None
         self._visited_ret_types = None
 
+    @if_wrap
     def visit_command(self, name, info, arg_type, ret_type,
-                      gen, success_response, boxed):
+                      gen, success_response, boxed, ifcond):
         if not gen:
             return
         self.decl += gen_command_decl(name, arg_type, boxed, ret_type)
scripts/qapi-event.py +3 -1
index bcbef1035f..cad9ece790 100644
@@ -152,6 +152,7 @@ class QAPISchemaGenEventVisitor(QAPISchemaVisitor):
         self.decl = None
         self.defn = None
         self._event_names = None
+        self.if_members = ['decl', 'defn']
 
     def visit_begin(self, schema):
         self.decl = ''
@@ -163,7 +164,8 @@ class QAPISchemaGenEventVisitor(QAPISchemaVisitor):
         self.defn += gen_enum_lookup(event_enum_name, self._event_names)
         self._event_names = None
 
-    def visit_event(self, name, info, arg_type, boxed):
+    @if_wrap
+    def visit_event(self, name, info, arg_type, boxed, ifcond):
         self.decl += gen_event_send_decl(name, arg_type, boxed)
         self.defn += gen_event_send(name, arg_type, boxed)
         self._event_names.append(name)
scripts/qapi-introspect.py +0 -0
index fc72cdb66d..ecfb0f2752 100644
@@ -12,7 +12,7 @@
 from qapi import *
 
 
Quoted 15 lines

scripts/qapi-introspect.py def to_qlit(obj, level=0, first_indent=True): · line 26

Quoted 13 lines
scripts/qapi-introspect.py +5 -1
-def to_qlit(obj, level=0, first_indent=True):
+def to_qlit(obj, level=0, first_indent=True, suffix=''):
     def indent(level):
         return level * 4 * ' '
     ret = ''
@@ -20,14 +20,20 @@ def to_qlit(obj, level=0, first_indent=True):
         ret += indent(level)
     if obj is None:
         ret += 'QLIT_QNULL'
+    elif isinstance(obj, tuple):
+        obj, ifcond =  obj
+        ret += gen_if(ifcond)
+        ret += to_qlit(obj, level, False) + suffix

Keyword argument instead of bare False, please.

ok

Quoted 12 lines

scripts/qapi-introspect.py def to_qlit(obj, level=0, first_indent=True): · line 28

Quoted 3 lines
scripts/qapi-introspect.py +2 -0
+        ret += gen_endif(ifcond)
+        suffix = ''

New case tuple, for generating conditionals. Okay.

scripts/qapi-introspect.py def to_qlit(obj, level=0, first_indent=True): · line 32

Quoted 5 lines
scripts/qapi-introspect.py +1 -1
     elif isinstance(obj, str):
         ret += 'QLIT_QSTR(' + '"' + obj.replace('"', r'\"') + '"' + ')'
     elif isinstance(obj, list):
-        elts = [to_qlit(elt, level + 1)
+        elts = [to_qlit(elt, level + 1, True, ",")
Quoted 3 lines

Make that

           elts = [to_qlit(elt, level + 1, suffix=",")

ok

Quoted 11 lines
Quoted 11 lines
scripts/qapi-introspect.py +1 -1
                 for elt in obj]
         elts.append(indent(level + 1) + "{ }")
         ret += 'QLIT_QLIST(((QLitObject[]) {\n'
-        ret += ',\n'.join(elts) + '\n'
+        ret += '\n'.join(elts) + '\n'
         ret += indent(level) + '}))'
     elif isinstance(obj, dict):
         elts = [ indent(level + 1) + '{ "%s", %s }' %
@@ -39,7 +45,7 @@ def to_qlit(obj, level=0, first_indent=True):
         ret += indent(level) + '}))'
Quoted 8 lines

scripts/qapi-introspect.py def to_qlit(obj, level=0, first_indent=True): · line 48

Quoted 4 lines
scripts/qapi-introspect.py +1 -1
     else:
         assert False                # not implemented
-    return ret
+    return ret + suffix

This is getting really hard to review --- my brain is about to overflow and shut down for the day. Can you split off some preparatory work? The introduction of suffix here, perhaps?

ok

Quoted 78 lines
Quoted 63 lines
scripts/qapi-introspect.py +14 -13
 class QAPISchemaGenIntrospectVisitor(QAPISchemaVisitor):
@@ -113,12 +119,12 @@ const QLitObject %(c_name)s = %(c_string)s;
             return '[' + self._use_type(typ.element_type) + ']'
         return self._name(typ.name)
 
-    def _gen_qlit(self, name, mtype, obj):
+    def _gen_qlit(self, name, mtype, obj, ifcond):
         if mtype not in ('command', 'event', 'builtin', 'array'):
             name = self._name(name)
         obj['name'] = name
         obj['meta-type'] = mtype
-        self._qlits.append(obj)
+        self._qlits.append((obj, ifcond))
 
     def _gen_member(self, member):
         ret = {'name': member.name, 'type': self._use_type(member.type)}
@@ -134,38 +140,40 @@ const QLitObject %(c_name)s = %(c_string)s;
         return {'case': variant.name, 'type': self._use_type(variant.type)}
 
     def visit_builtin_type(self, name, info, json_type):
-        self._gen_qlit(name, 'builtin', {'json-type': json_type})
+        self._gen_qlit(name, 'builtin', {'json-type': json_type}, None)
 
-    def visit_enum_type(self, name, info, values, prefix):
-        self._gen_qlit(name, 'enum', {'values': values})
+    def visit_enum_type(self, name, info, values, prefix, ifcond):
+        self._gen_qlit(name, 'enum', {'values': values}, ifcond)
 
-    def visit_array_type(self, name, info, element_type):
+    def visit_array_type(self, name, info, element_type, ifcond):
         element = self._use_type(element_type)
-        self._gen_qlit('[' + element + ']', 'array', {'element-type': element})
+        self._gen_qlit('[' + element + ']', 'array', {'element-type': element},
+                       ifcond)
 
-    def visit_object_type_flat(self, name, info, members, variants):
+    def visit_object_type_flat(self, name, info, members, variants, ifcond):
         obj = {'members': [self._gen_member(m) for m in members]}
         if variants:
             obj.update(self._gen_variants(variants.tag_member.name,
                                           variants.variants))
-        self._gen_qlit(name, 'object', obj)
+        self._gen_qlit(name, 'object', obj, ifcond)
 
-    def visit_alternate_type(self, name, info, variants):
+    def visit_alternate_type(self, name, info, variants, ifcond):
         self._gen_qlit(name, 'alternate',
                        {'members': [{'type': self._use_type(m.type)}
-                                    for m in variants.variants]})
+                                    for m in variants.variants]}, ifcond)
 
     def visit_command(self, name, info, arg_type, ret_type,
-                      gen, success_response, boxed):
+                      gen, success_response, boxed, ifcond):
         arg_type = arg_type or self._schema.the_empty_object_type
         ret_type = ret_type or self._schema.the_empty_object_type
         self._gen_qlit(name, 'command',
                        {'arg-type': self._use_type(arg_type),
-                        'ret-type': self._use_type(ret_type)})
+                        'ret-type': self._use_type(ret_type)}, ifcond)

scripts/qapi-introspect.py const QLitObject %(c_name)s = %(c_string)s; · line 179

Quoted 10 lines
scripts/qapi-introspect.py +3 -2
 
-    def visit_event(self, name, info, arg_type, boxed):
+    def visit_event(self, name, info, arg_type, boxed, ifcond):
         arg_type = arg_type or self._schema.the_empty_object_type
-        self._gen_qlit(name, 'event', {'arg-type': self._use_type(arg_type)})
+        self._gen_qlit(name, 'event', {'arg-type': self._use_type(arg_type)},
+                       ifcond)
 
 # Debugging aid: unmask QAPI schema's type names
 # We normally mask them, because they're not QMP wire ABI

Out of review brainpower for today. Hope to resume tomorrow.

[...]

Signature · 1 line
Marc-André Lureau
#22 lore raw

Re: [Qemu-devel] [PATCH 07/26] qapi: add 'if' condition on top-level schema elements

Quoted 425 lines · Marc-André Lureau

Hi

Quoted 8 lines · Markus Armbruster
Quoted 2 lines · Marc-André Lureau

Add 'if' c-preprocessor condition on top-level schema elements:
struct, enum, union, alternate, command, event.

An example would be useful here. Your cover letter has nice ones, would be a shame not to preserve them for posterity in the commit log.

ok

Quoted 4 lines
Quoted 1 line

Variants objects types are created outside of #if blocks, since they

What are "variants objects types"?

I think I meant implicit objects types, that may be created by variant.

Quoted 44 lines
Quoted 4 lines

may be shared by various types. Even if unused, this shouldn't be an issue, since those are internal types.

Note that there is no checks in qapi scripts to verify that elements

there are

Quoted 3 lines

have compatible conditions (ex: if-struct used by a if-foo command). This may thus fail at C build time if they don't share the same subset of conditions.

Fair enough.

Quoted 31 lines
---
 scripts/qapi.py                         | 153 +++++++++++++++++++++++++-------
 scripts/qapi-commands.py                |   4 +-
 scripts/qapi-event.py                   |   4 +-
 scripts/qapi-introspect.py              |  46 ++++++----
 scripts/qapi-types.py                   |  51 +++++++----
 scripts/qapi-visit.py                   |  13 ++-
 scripts/qapi2texi.py                    |  10 +--
 tests/Makefile.include                  |   1 +
 tests/qapi-schema/bad-if.err            |   1 +
 tests/qapi-schema/bad-if.exit           |   1 +
 tests/qapi-schema/bad-if.json           |   3 +
 tests/qapi-schema/bad-if.out            |   0
 tests/qapi-schema/qapi-schema-test.json |  20 +++++
 tests/qapi-schema/qapi-schema-test.out  |  31 +++++++
 tests/qapi-schema/test-qapi.py          |  21 +++--
 15 files changed, 272 insertions(+), 87 deletions(-)
 create mode 100644 tests/qapi-schema/bad-if.err
 create mode 100644 tests/qapi-schema/bad-if.exit
 create mode 100644 tests/qapi-schema/bad-if.json
 create mode 100644 tests/qapi-schema/bad-if.out
scripts/qapi.py +0 -0
index 4ecc19e944..79ba1e93da 100644
@@ -639,6 +639,16 @@ def add_name(name, info, meta, implicit=False):
     all_names[name] = meta
 
 
Quoted 11 lines

scripts/qapi.py def add_name(name, info, meta, implicit=False): · line 649

Quoted 8 lines
scripts/qapi.py +8 -0
+def check_if(expr, info):
+    ifcond = expr.get('if')
+    if not ifcond or isinstance(ifcond, str):
+        return
+    if (not isinstance(ifcond, list) or
+        any([not isinstance(elt, str) for elt in ifcond])):
+        raise QAPISemError(info, "'if' condition requires a string or "
+                           "a list of string")

Wait a second! What's this list business? The commit message doesn't say.

Updated commit message, and documented in docs/devel/qapi-code-gen.txt

Quoted 5 lines

Also, pep8 gripes:

scripts/qapi.py:647:9: E129 visually indented line with same indent as next logical line

fixed

Quoted 26 lines

scripts/qapi.py def check_keys(expr_elem, meta, required, optional=[]): · line 878

Quoted 10 lines
scripts/qapi.py +3 -0
+
+
 def check_type(info, source, value, allow_array=False,
                allow_dict=False, allow_optional=False,
                allow_metas=[]):
@@ -865,6 +875,7 @@ def check_keys(expr_elem, meta, required, optional=[]):
     expr = expr_elem['expr']
     info = expr_elem['info']
     name = expr[meta]
+    optional.append('if')

Caution!

    $ python
    Python 2.7.13 (default, May 10 2017, 20:04:36)
Quoted 1 line
Quoted 1 line
Quoted 1 line

def surprise(arg=[]):

    ...     arg.append('if')
    ...     return arg
    ...
    >>> surprise()
    ['if']
    >>> surprise()
    ['if', 'if']
    >>> surprise()
    ['if', 'if', 'if']
Quoted 2 lines

Never modify an argument that has list or dictionary default value. To avoid the temptation, never use such defaul values.

Oops! Without bad consequences here, but fixed anyway.

Quoted 15 lines
Quoted 15 lines

if not isinstance(name, str):
raise QAPISemError(info, "'%s' key must have a string value" % meta)
required = required + [meta]

scripts/qapi.py +2 -0
@@ -880,6 +891,8 @@ def check_keys(expr_elem, meta, required, optional=[]):
             raise QAPISemError(info,
                                "'%s' of %s '%s' should only use true value"
                                % (key, meta, name))
+        if key == 'if':
+            check_if(expr, info)
     for key in required:
         if key not in expr:
             raise QAPISemError(info, "Key '%s' is missing from %s '%s'"
@@ -989,6 +1002,10 @@ class QAPISchemaEntity(object):
         # such place).
Quoted 8 lines

scripts/qapi.py class QAPISchemaEntity(object): · line 1008

Quoted 6 lines
scripts/qapi.py +4 -0
         self.info = info
         self.doc = doc
+        self.ifcond = None
+
+    def set_ifcond(self, ifcond):
+        self.ifcond = ifcond

@ifcond is an awkward name, but I don't have better ideas.

Yeah, I got used to it now ;)

Quoted 79 lines
Quoted 66 lines
scripts/qapi.py +11 -9
     def c_name(self):
         return c_name(self.name)
@@ -1017,26 +1034,26 @@ class QAPISchemaVisitor(object):
     def visit_builtin_type(self, name, info, json_type):
         pass
 
-    def visit_enum_type(self, name, info, values, prefix):
+    def visit_enum_type(self, name, info, values, prefix, ifcond):
         pass
 
-    def visit_array_type(self, name, info, element_type):
+    def visit_array_type(self, name, info, element_type, ifcond):
         pass
 
-    def visit_object_type(self, name, info, base, members, variants):
+    def visit_object_type(self, name, info, base, members, variants, ifcond):
         pass
 
-    def visit_object_type_flat(self, name, info, members, variants):
+    def visit_object_type_flat(self, name, info, members, variants, ifcond):
         pass
 
-    def visit_alternate_type(self, name, info, variants):
+    def visit_alternate_type(self, name, info, variants, ifcond):
         pass
 
     def visit_command(self, name, info, arg_type, ret_type,
-                      gen, success_response, boxed):
+                      gen, success_response, boxed, ifcond):
         pass
 
-    def visit_event(self, name, info, arg_type, boxed):
+    def visit_event(self, name, info, arg_type, boxed, ifcond):
         pass
 
 
@@ -1136,7 +1153,7 @@ class QAPISchemaEnumType(QAPISchemaType):
 
     def visit(self, visitor):
         visitor.visit_enum_type(self.name, self.info,
-                                self.member_names(), self.prefix)
+                                self.member_names(), self.prefix, self.ifcond)
 
 
 class QAPISchemaArrayType(QAPISchemaType):
@@ -1149,6 +1166,7 @@ class QAPISchemaArrayType(QAPISchemaType):
     def check(self, schema):
         self.element_type = schema.lookup_type(self._element_type_name)
         assert self.element_type
+        self.ifcond = self.element_type.ifcond
 
     def is_implicit(self):
         return True
@@ -1166,7 +1184,8 @@ class QAPISchemaArrayType(QAPISchemaType):
         return 'array of ' + elt_doc_type
 
     def visit(self, visitor):
-        visitor.visit_array_type(self.name, self.info, self.element_type)
+        visitor.visit_array_type(self.name, self.info, self.element_type,
+                                 self.ifcond)
 
 
 class QAPISchemaObjectType(QAPISchemaType):

scripts/qapi.py class QAPISchemaObjectType(QAPISchemaType): · line 1272

Quoted 9 lines
scripts/qapi.py +3 -2
@@ -1247,9 +1266,10 @@ class QAPISchemaObjectType(QAPISchemaType):
 
     def visit(self, visitor):
         visitor.visit_object_type(self.name, self.info,
-                                  self.base, self.local_members, self.variants)
+                                  self.base, self.local_members, self.variants,
+                                  self.ifcond)
         visitor.visit_object_type_flat(self.name, self.info,
-                                       self.members, self.variants)
+                                       self.members, self.variants, self.ifcond)

You break the line before self.ifcond almost everywhere, and when you don't, the line gets long. This one goes over the limit:

Quoted 4 lines
    scripts/qapi.py:1285:80: E501 line too long (80 > 79 characters)

Suggest to break it before self.ifcond everywhere.

ok

Quoted 56 lines
Quoted 34 lines
scripts/qapi.py +6 -3
 class QAPISchemaMember(object):
@@ -1392,7 +1412,8 @@ class QAPISchemaAlternateType(QAPISchemaType):
         return 'value'
 
     def visit(self, visitor):
-        visitor.visit_alternate_type(self.name, self.info, self.variants)
+        visitor.visit_alternate_type(self.name, self.info,
+                                     self.variants, self.ifcond)
 
     def is_empty(self):
         return False
@@ -1434,7 +1455,8 @@ class QAPISchemaCommand(QAPISchemaEntity):
     def visit(self, visitor):
         visitor.visit_command(self.name, self.info,
                               self.arg_type, self.ret_type,
-                              self.gen, self.success_response, self.boxed)
+                              self.gen, self.success_response, self.boxed,
+                              self.ifcond)
 
 
 class QAPISchemaEvent(QAPISchemaEntity):
@@ -1462,7 +1484,8 @@ class QAPISchemaEvent(QAPISchemaEntity):
             raise QAPISemError(self.info, "Use of 'boxed' requires 'data'")
 
     def visit(self, visitor):
-        visitor.visit_event(self.name, self.info, self.arg_type, self.boxed)
+        visitor.visit_event(self.name, self.info, self.arg_type, self.boxed,
+                            self.ifcond)
 
 
 class QAPISchema(object):

scripts/qapi.py class QAPISchema(object): · line 1512

Quoted 10 lines
scripts/qapi.py +2 -1
@@ -1481,11 +1504,12 @@ class QAPISchema(object):
             print >>sys.stderr, err
             exit(1)
 
-    def _def_entity(self, ent):
+    def _def_entity(self, ent, ifcond=None):
         # Only the predefined types are allowed to not have info
         assert ent.info or self._predefining
         assert ent.name not in self._entity_dict
         self._entity_dict[ent.name] = ent
+        ent.set_ifcond(ifcond)

.set_ifcond(None) does the right thing.

However:

Quoted 7 lines
scripts/qapi.py +0 -0
     def lookup_entity(self, name, typ=None):
         ent = self._entity_dict.get(name)
@@ -1534,11 +1558,11 @@ class QAPISchema(object):
     def _make_enum_members(self, values):
         return [QAPISchemaMember(v) for v in values]
 
Quoted 12 lines

scripts/qapi.py class QAPISchema(object): · line 1566

Quoted 8 lines
scripts/qapi.py +2 -2
-    def _make_implicit_enum_type(self, name, info, values):
+    def _make_implicit_enum_type(self, name, info, values, ifcond):
         # See also QAPISchemaObjectTypeMember._pretty_owner()
         name = name + 'Kind'   # Use namespace reserved by add_name()
         self._def_entity(QAPISchemaEnumType(
-            name, info, None, self._make_enum_members(values), None))
+            name, info, None, self._make_enum_members(values), None), ifcond)
         return name

Why is ifcond not a constructor argument like name, info, and so forth? What makes it special?

I think it was easier that way (builder pattern), but it make sense as constructor too. Changed

Quoted 47 lines
Quoted 11 lines
scripts/qapi.py +2 -1
     def _make_array_type(self, element_type, info):
@@ -1547,22 +1571,26 @@ class QAPISchema(object):
             self._def_entity(QAPISchemaArrayType(name, info, element_type))
         return name
 
-    def _make_implicit_object_type(self, name, info, doc, role, members):
+    def _make_implicit_object_type(self, name, info, doc, role, members,
+                                   ifcond=None):
         if not members:
             return None

scripts/qapi.py class QAPISchema(object): · line 1585

Quoted 10 lines
scripts/qapi.py +4 -2
         # See also QAPISchemaObjectTypeMember._pretty_owner()
         name = 'q_obj_%s-%s' % (name, role)
-        if not self.lookup_entity(name, QAPISchemaObjectType):
+        if self.lookup_entity(name, QAPISchemaObjectType):
+            assert ifcond is None
+        else:
             self._def_entity(QAPISchemaObjectType(name, info, doc, None,
-                                                  members, None))
+                                                  members, None), ifcond)
         return name

Hmm, this smells like it might be the "variants objects types" mentioned in the commit message.

Types made with _make_implicit_object_type():

* The wrapper around "simple" union members, by _make_simple_variant()

* A flat union's base type when it's implicit, by _def_union_type()

* A command's argument type when it's implicit, by _def_command()

* An event's argument type when it's implicit, by _def_event()

Only the first one can be used more than once, namely when a type occurs in more than one simple union. The "correct" ifcond is the disjunction of all its user's ifconds. You make it use the *first* ifcond instead.
Wrong: breaks when one of the other simple unions has a condition that is true when the first one's is false.

Your commit message suggests you intended to make it unconditional instead. That would work: the worst that can happen is compiling a few q_obj_FOO_wrapper typedefs and visit_type_q_FOO_wrapper() functions that aren't actually used. Tolerable, in particular since I hope to get rid of "simple" unions some day.

Quoted 6 lines

Sadly, it would prevent us from making the visit functions for implicit types static, because unused static functions trigger warnings. Let's not worry about that now.

Generating the disjunction of all conditions wouldn't be terribly hard. I'm not asking for it, though.

I suggest to tackle it as follow-up then. Added a FIXME

Quoted 9 lines

You assert that implicit types are unconditional from the second use on. I guess you mean to assert the ones used more than once are unconditional:

typ = self.lookup_entity(name, QAPISchemaObjectType) if typ:
assert ifcond is None and typ.ifcond is None

Quoted 7 lines

But what you *should* assert is that the conditions are the same:

typ = self.lookup_entity(name, QAPISchemaObjectType) if typ:
assert ifcond == typ.ifcond

Quoted 1 line

ok

Quoted 10 lines

scripts/qapi.py class QAPISchema(object): · line 1593

Quoted 9 lines

def _def_enum_type(self, expr, info, doc):
name = expr['enum']
data = expr['data']
prefix = expr.get('prefix')

scripts/qapi.py +3 -2
-        self._def_entity(QAPISchemaEnumType(
-            name, info, doc, self._make_enum_members(data), prefix))
+        return self._def_entity(QAPISchemaEnumType(
+            name, info, doc, self._make_enum_members(data), prefix),
+                                expr.get('if'))
Quoted 3 lines

Covers enumeration types.

Why return? The (only) caller throws the value away...

left-over, fixed

Quoted 31 lines

scripts/qapi.py class QAPISchema(object): · line 1616

Quoted 11 lines

def _make_member(self, name, typ, info):
optional = False

scripts/qapi.py +2 -1
@@ -1584,7 +1612,8 @@ class QAPISchema(object):
         data = expr['data']
         self._def_entity(QAPISchemaObjectType(name, info, doc, base,
                                               self._make_members(data, info),
-                                              None))
+                                              None),
+                         expr.get('if'))

Covers struct types.

scripts/qapi.py class QAPISchema(object): · line 1629

Quoted 13 lines
scripts/qapi.py +3 -1
     def _make_variant(self, case, typ):
         return QAPISchemaObjectTypeVariant(case, typ)
@@ -1593,8 +1622,10 @@ class QAPISchema(object):
         if isinstance(typ, list):
             assert len(typ) == 1
             typ = self._make_array_type(typ[0], info)
+        type_entity = self.lookup_type(typ)
         typ = self._make_implicit_object_type(
-            typ, info, None, 'wrapper', [self._make_member('data', typ, info)])
+            typ, info, None, 'wrapper',
+            [self._make_member('data', typ, info)], type_entity.ifcond)
         return QAPISchemaObjectTypeVariant(case, typ)

A simple union member's wrapper type inherits its condition from the member type.

Quoted 4 lines
Quoted 1 line

I think you need to pass None instead of type_entity.ifcond here.

That doesn't work, visitor symbols are missing in generated code. Why shouldn't the wrapper share the same condition as the underlying type?

Because it's shared with other simple unions that have the same variant?

Quoted 128 lines
Quoted 39 lines

scripts/qapi.py class QAPISchema(object): · line 1640

Quoted 11 lines
scripts/qapi.py +3 -2
     def _def_union_type(self, expr, info, doc):
@@ -1604,8 +1635,9 @@ class QAPISchema(object):
         tag_name = expr.get('discriminator')
         tag_member = None
         if isinstance(base, dict):
-            base = (self._make_implicit_object_type(
-                name, info, doc, 'base', self._make_members(base, info)))
+            base = self._make_implicit_object_type(
+                name, info, doc, 'base', self._make_members(base, info),
+                expr.get('if'))

A flat union's implicit base type inherits its condition from the flat union. Good.

scripts/qapi.py class QAPISchema(object): · line 1650

Quoted 10 lines
scripts/qapi.py +2 -1
         if tag_name:
             variants = [self._make_variant(key, value)
                         for (key, value) in data.iteritems()]
@@ -1614,14 +1646,16 @@ class QAPISchema(object):
             variants = [self._make_simple_variant(key, value, info)
                         for (key, value) in data.iteritems()]
             typ = self._make_implicit_enum_type(name, info,
-                                                [v.name for v in variants])
+                                                [v.name for v in variants],
+                                                expr.get('if'))

A flat union's implicit enumeration type inherits its condition from the flat union. Good.

scripts/qapi.py class QAPISchema(object): · line 1658

Quoted 9 lines
scripts/qapi.py +2 -1
             tag_member = QAPISchemaObjectTypeMember('type', typ, False)
             members = [tag_member]
         self._def_entity(
             QAPISchemaObjectType(name, info, doc, base, members,
                                  QAPISchemaObjectTypeVariants(tag_name,
                                                               tag_member,
-                                                              variants)))
+                                                              variants)),
+            expr.get('if'))
Quoted 7 lines

Covers union types.

Third use of expr.get('if') in this function. Please put it in a variable.

Actually, do that for *all* uses of expr[X] and expr.get(X) in class QAPISchema, because that's how the existing code works.

done

Quoted 65 lines

scripts/qapi.py class QAPISchema(object): · line 1671

Quoted 11 lines
scripts/qapi.py +2 -1
     def _def_alternate_type(self, expr, info, doc):
         name = expr['alternate']
@@ -1633,7 +1667,8 @@ class QAPISchema(object):
             QAPISchemaAlternateType(name, info, doc,
                                     QAPISchemaObjectTypeVariants(None,
                                                                  tag_member,
-                                                                 variants)))
+                                                                 variants)),
+            expr.get('if'))

Covers alternate types.

scripts/qapi.py class QAPISchema(object): · line 1683

Quoted 10 lines
scripts/qapi.py +2 -1
     def _def_command(self, expr, info, doc):
         name = expr['command']
@@ -1644,12 +1679,14 @@ class QAPISchema(object):
         boxed = expr.get('boxed', False)
         if isinstance(data, OrderedDict):
             data = self._make_implicit_object_type(
-                name, info, doc, 'arg', self._make_members(data, info))
+                name, info, doc, 'arg', self._make_members(data, info),
+                expr.get('if'))

A command's implicit argument type inherits its condition from the command. Good.

scripts/qapi.py class QAPISchema(object): · line 1689

Quoted 7 lines
scripts/qapi.py +2 -1
         if isinstance(rets, list):
             assert len(rets) == 1
             rets = self._make_array_type(rets[0], info)
         self._def_entity(QAPISchemaCommand(name, info, doc, data, rets,
-                                           gen, success_response, boxed))
+                                           gen, success_response, boxed),
+                         expr.get('if'))

Covers commands.

scripts/qapi.py class QAPISchema(object): · line 1698

Quoted 11 lines
scripts/qapi.py +2 -2
     def _def_event(self, expr, info, doc):
         name = expr['event']
@@ -1657,8 +1694,10 @@ class QAPISchema(object):
         boxed = expr.get('boxed', False)
         if isinstance(data, OrderedDict):
             data = self._make_implicit_object_type(
-                name, info, doc, 'arg', self._make_members(data, info))
-        self._def_entity(QAPISchemaEvent(name, info, doc, data, boxed))
+                name, info, doc, 'arg', self._make_members(data, info),
+                expr.get('if'))

An event's implicit argument type inherits its condition from the command. Good.

scripts/qapi.py class QAPISchema(object): · line 1700

Quoted 2 lines
scripts/qapi.py +2 -0
+        self._def_entity(QAPISchemaEvent(name, info, doc, data, boxed),
+                         expr.get('if'))

Covers events. You got them all. Good.

Quoted 7 lines
scripts/qapi.py +0 -0
     def _def_exprs(self):
         for expr_elem in self.exprs:
@@ -1848,6 +1887,54 @@ def guardend(name):
                  name=guardname(name))
 
 
Quoted 13 lines

scripts/qapi.py def guardend(name): · line 1899

Quoted 10 lines
scripts/qapi.py +10 -0
+def gen_if(ifcond, func=''):
+    if not ifcond:
+        return ''
+    if isinstance(ifcond, str):
+        ifcond = [ifcond]
+    ret = '\n'
+    for ifc in ifcond:
+        ret += mcgen('#if %(ifcond)s /* %(func)s */\n', ifcond=ifc, func=func)
+    ret += '\n'
+    return ret

Please use mcgen() like the existing code:

Quoted 1 line

mcgen() does indent and fails with pre-processor lines. I added a comment

I see. Comment is good enough for now.

Quoted 87 lines
Quoted 9 lines
           ret += mcgen('''
   #if %(ifcond)s /* %(func)s */
   ''', ifcond=ifc, func=func)

With the default value of @func, we get a useless, ugly comment /* */. If this can happen, please suppress the comment. Else, drop @func's default value.

Lists appear to be conjunctions. What for?

I added some doc in qapi-code-gen.txt

Quoted 17 lines
Quoted 4 lines
scripts/qapi.py +3 -0
+
+
+def gen_endif(ifcond, func=''):

scripts/qapi.py def guardend(name): · line 1912

Quoted 10 lines
scripts/qapi.py +10 -0
+    if not ifcond:
+        return ''
+    if isinstance(ifcond, str):
+        ifcond = [ifcond]
+    ret = '\n'
+    for ifc in reversed(ifcond):
+        ret += mcgen('#endif /* %(ifcond)s %(func)s */\n',
+                     ifcond=ifc, func=func)
+    ret += '\n'
+    return ret

Likewise.

Quoted 8 lines

scripts/qapi.py def guardend(name): · line 1917

Quoted 5 lines
scripts/qapi.py +5 -0
+
+
+# wrap a method to add #if / #endif to generated code
+# self must have 'if_members' listing the attributes to wrap
+# the last argument of the wrapped function must be the 'ifcond'

Start your sentences with a capital letter, and end them with a period, please.

ok

Quoted 4 lines

scripts/qapi.py def guardend(name): · line 1918

Quoted 2 lines
scripts/qapi.py +1 -0
+def if_wrap(func):

Blank line, please.

ok

Quoted 15 lines
Quoted 4 lines
scripts/qapi.py +3 -0
+    def func_wrapper(self, *args, **kwargs):
+        funcname = self.__class__.__name__ + '.' + func.__name__
+        ifcond = args[-1]

scripts/qapi.py def guardend(name): · line 1931

Quoted 10 lines
scripts/qapi.py +10 -0
+        save = {}
+        for mem in self.if_members:
+            save[mem] = getattr(self, mem)
+        func(self, *args, **kwargs)
+        for mem, val in save.items():
+            newval = getattr(self, mem)
+            if newval != val:
+                assert newval.startswith(val)
+                newval = newval[len(val):]
+                val += gen_if(ifcond, funcname)
Quoted 3 lines

Emitting comments pointing to the QAPI schema into the generated code is often helpful. But this one points to QAPI generator code. Why is that useful?

That was mostly useful during development, removed

Quoted 10 lines

scripts/qapi.py def guardend(name): · line 1937

Quoted 7 lines
scripts/qapi.py +6 -0
+                val += newval
+                val += gen_endif(ifcond, funcname)
+            setattr(self, mem, val)
+
+    return func_wrapper
+

pep8 again:

Quoted 5 lines
    scripts/qapi.py:1955:1: E302 expected 2 blank lines, found 1

Peeking ahead: this function is used as a decorator. Let's help the reader and mention that in the function comment, or by naming the function suitably. ifcond_decorator?

done

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

Messing with the wrapped method's class's attributes is naughty. Worse, it's hard to understand. What alternatives have you considered?

Well, I started writing the code that checked if members got code added, I had to put some enter()/leave() code everywhere. Then I realize this could easily be the job for a decorator. I think the end result is pretty neat.

I think "clever" would describe it better than "neat". Possibly "too clever".

Quoted 2 lines

If you have a better idea, can you do it in a
follow-up?

I need to complete review before I can tell.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
scripts/qapi.py +0 -0
 def gen_enum_lookup(name, values, prefix=None):
     ret = mcgen('''
 

[...]

#23 lore raw

Re: [Qemu-devel] [PATCH 07/26] qapi: add 'if' condition on top-level schema elements

Quoted 1 line · Markus Armbruster

Marc-André Lureau <[email protected]> writes:

[...]

Quoted 42 lines
Quoted 42 lines
Quoted 14 lines

scripts/qapi.py def guardend(name): · line 1931

Quoted 13 lines
scripts/qapi.py +13 -0
+    def func_wrapper(self, *args, **kwargs):
+        funcname = self.__class__.__name__ + '.' + func.__name__
+        ifcond = args[-1]
+        save = {}
+        for mem in self.if_members:
+            save[mem] = getattr(self, mem)
+        func(self, *args, **kwargs)
+        for mem, val in save.items():
+            newval = getattr(self, mem)
+            if newval != val:
+                assert newval.startswith(val)
+                newval = newval[len(val):]
+                val += gen_if(ifcond, funcname)
Quoted 3 lines

Emitting comments pointing to the QAPI schema into the generated code is often helpful. But this one points to QAPI generator code. Why is that useful?

That was mostly useful during development, removed

Quoted 10 lines

scripts/qapi.py def guardend(name): · line 1937

Quoted 7 lines
scripts/qapi.py +6 -0
+                val += newval
+                val += gen_endif(ifcond, funcname)
+            setattr(self, mem, val)
+
+    return func_wrapper
+

pep8 again:

Quoted 5 lines
    scripts/qapi.py:1955:1: E302 expected 2 blank lines, found 1

Peeking ahead: this function is used as a decorator. Let's help the reader and mention that in the function comment, or by naming the function suitably. ifcond_decorator?

done

Quoted 3 lines

Messing with the wrapped method's class's attributes is naughty. Worse, it's hard to understand. What alternatives have you considered?

Quoted 7 lines
Quoted 4 lines

Well, I started writing the code that checked if members got code added, I had to put some enter()/leave() code everywhere. Then I realize this could easily be the job for a decorator. I think the end result is pretty neat.

I think "clever" would describe it better than "neat". Possibly "too clever".

FWIW, I was investigating something else in the series and took a while to understand how the #if lines were magically appearing on self.decl and self.defn.

I'd prefer something simpler like:

def cond(ifcond, s):
if s:
return gen_if(ifcond) + s + gen_endif(ifcond)
return s

def visit_command(self, name, info, arg_type, ret_type,
gen, success_response, boxed, ifcond):
if not gen:
return
self.decl += cond(ifcond, gen_command_decl(name, arg_type, boxed, ret_type)) if ret_type and ret_type not in self._visited_ret_types:
self._visited_ret_types.add(ret_type)
self.defn += cond(ifcond,gen_marshal_output(ret_type))
self.decl += cond(ifcond, gen_marshal_decl(name))
self.defn += cond(ifcond, gen_marshal(name, arg_type, boxed, ret_type)) self._regy += cond(ifcond, gen_register_command(name, success_response))

If all callers of some of the gen_*() functions above are wrapped by ifcond(), then a decorator around them could still be useful. But preferably if the decorator affects only the function arguments and return value, not messing with the object attributes or other state outside the function.

Signature · 1 line
Eduardo
#24 lore raw

Re: [Qemu-devel] [PATCH 07/26] qapi: add 'if' condition on top-level schema elements

In reply to Marc-André Lureau
Quoted 26 lines · Marc-André Lureau

Add 'if' c-preprocessor condition on top-level schema elements:
struct, enum, union, alternate, command, event.

Variants objects types are created outside of #if blocks, since they may be shared by various types. Even if unused, this shouldn't be an issue, since those are internal types.

Note that there is no checks in qapi scripts to verify that elements have compatible conditions (ex: if-struct used by a if-foo command). This may thus fail at C build time if they don't share the same subset of conditions.

---
 scripts/qapi.py                         | 153 +++++++++++++++++++++++++-------
 scripts/qapi-commands.py                |   4 +-
 scripts/qapi-event.py                   |   4 +-
 scripts/qapi-introspect.py              |  46 ++++++----
 scripts/qapi-types.py                   |  51 +++++++----
 scripts/qapi-visit.py                   |  13 ++-
 scripts/qapi2texi.py                    |  10 +--
 tests/Makefile.include                  |   1 +
 tests/qapi-schema/bad-if.err            |   1 +
 tests/qapi-schema/bad-if.exit           |   1 +
 tests/qapi-schema/bad-if.json           |   3 +
 tests/qapi-schema/bad-if.out            |   0
Quoted 8 lines
 tests/qapi-schema/qapi-schema-test.json |  20 +++++
 tests/qapi-schema/qapi-schema-test.out  |  31 +++++++
 tests/qapi-schema/test-qapi.py          |  21 +++--
 15 files changed, 272 insertions(+), 87 deletions(-)
 create mode 100644 tests/qapi-schema/bad-if.err
 create mode 100644 tests/qapi-schema/bad-if.exit
 create mode 100644 tests/qapi-schema/bad-if.json
 create mode 100644 tests/qapi-schema/bad-if.out

Missing: docs/devel/qapi-code-gen.txt update.

[...]

#25 lore raw

PATCH 8/26 qapi: add 'if' condition on enum member values

In reply to Marc-André Lureau
---
 scripts/qapi.py                                 | 39 +++++++++++++++++++++----
 tests/Makefile.include                          |  2 ++
 tests/qapi-schema/enum-dict-member-invalid.err  |  1 +
 tests/qapi-schema/enum-dict-member-invalid.exit |  1 +
 tests/qapi-schema/enum-dict-member-invalid.json |  2 ++
 tests/qapi-schema/enum-dict-member-invalid.out  |  0
 tests/qapi-schema/enum-dict-member.err          |  1 -
 tests/qapi-schema/enum-dict-member.exit         |  2 +-
 tests/qapi-schema/enum-dict-member.json         |  3 +-
 tests/qapi-schema/enum-dict-member.out          |  4 +++
 tests/qapi-schema/enum-if-invalid.err           |  1 +
 tests/qapi-schema/enum-if-invalid.exit          |  1 +
 tests/qapi-schema/enum-if-invalid.json          |  3 ++
 tests/qapi-schema/enum-if-invalid.out           |  0
 tests/qapi-schema/qapi-schema-test.json         |  5 ++--
 tests/qapi-schema/qapi-schema-test.out          |  3 +-
 tests/qapi-schema/test-qapi.py                  |  4 ++-
 17 files changed, 58 insertions(+), 14 deletions(-)
 create mode 100644 tests/qapi-schema/enum-dict-member-invalid.err
 create mode 100644 tests/qapi-schema/enum-dict-member-invalid.exit
 create mode 100644 tests/qapi-schema/enum-dict-member-invalid.json
 create mode 100644 tests/qapi-schema/enum-dict-member-invalid.out
 create mode 100644 tests/qapi-schema/enum-if-invalid.err
 create mode 100644 tests/qapi-schema/enum-if-invalid.exit
 create mode 100644 tests/qapi-schema/enum-if-invalid.json
 create mode 100644 tests/qapi-schema/enum-if-invalid.out
scripts/qapi.py +33 -6
index 79ba1e93da..93d07283e6 100644
@@ -729,6 +729,10 @@ def check_event(expr, info):
                allow_metas=meta)
 
 
+def enum_get_values(expr):
+    return [e if isinstance(e, str) else e['name'] for e in expr['data']]
+
+
 def check_union(expr, info):
     name = expr['union']
     base = expr.get('base')
@@ -788,7 +792,7 @@ def check_union(expr, info):
         # If the discriminator names an enum type, then all members
         # of 'data' must also be members of the enum type.
         if enum_define:
-            if key not in enum_define['data']:
+            if key not in enum_get_values(enum_define):
                 raise QAPISemError(info,
                                    "Discriminator value '%s' is not found in "
                                    "enum '%s'"
@@ -796,7 +800,7 @@ def check_union(expr, info):
 
     # If discriminator is user-defined, ensure all values are covered
     if enum_define:
-        for value in enum_define['data']:
+        for value in enum_get_values(enum_define):
             if value not in members.keys():
                 raise QAPISemError(info, "Union '%s' data missing '%s' branch"
                                    % (name, value))
@@ -827,7 +831,7 @@ def check_alternate(expr, info):
         if qtype == 'QTYPE_QSTRING':
             enum_expr = enum_types.get(value)
             if enum_expr:
-                for v in enum_expr['data']:
+                for v in enum_get_values(enum_expr):
                     if v in ['on', 'off']:
                         conflicting.add('QTYPE_QBOOL')
                     if re.match(r'[-+0-9.]', v): # lazy, could be tightened
@@ -857,6 +861,12 @@ def check_enum(expr, info):
         raise QAPISemError(info,
                            "Enum '%s' requires a string for 'prefix'" % name)
     for member in members:
+        if isinstance(member, dict):
+            if not 'name' in member:
+                raise QAPISemError(info, "Dictionary member of enum '%s' must "
+                                   "have a 'name' key" % name)
+            check_if(member, info)
+            member = member['name']
         check_name(info, "Member of enum '%s'" % name, member,
                    enum_member=True)
 
@@ -1148,12 +1158,15 @@ class QAPISchemaEnumType(QAPISchemaType):
     def member_names(self):
         return [v.name for v in self.values]
 
+    def members(self):
+        return [(v.name, v.ifcond) for v in self.values]
+
     def json_type(self):
         return 'string'
 
     def visit(self, visitor):
         visitor.visit_enum_type(self.name, self.info,
-                                self.member_names(), self.prefix, self.ifcond)
+                                self.members(), self.prefix, self.ifcond)
 
 
 class QAPISchemaArrayType(QAPISchemaType):
@@ -1275,9 +1288,11 @@ class QAPISchemaObjectType(QAPISchemaType):
 class QAPISchemaMember(object):
     role = 'member'
 
-    def __init__(self, name):
+    def __init__(self, name, ifcond=None):
         assert isinstance(name, str)
+        assert ifcond is None or isinstance(ifcond, str)
         self.name = name
+        self.ifcond = ifcond
         self.owner = None
 
     def set_owner(self, name):
@@ -1556,7 +1571,9 @@ class QAPISchema(object):
                                             qtype_values, 'QTYPE'))
 
     def _make_enum_members(self, values):
-        return [QAPISchemaMember(v) for v in values]
+        return [QAPISchemaMember(v['name'], v.get('if')) if isinstance(v, dict)
+                else QAPISchemaMember(v)
+                for v in values]
 
     def _make_implicit_enum_type(self, name, info, values, ifcond):
         # See also QAPISchemaObjectTypeMember._pretty_owner()
@@ -1942,11 +1959,16 @@ const char *const %(c_name)s_lookup[] = {
 ''',
                 c_name=c_name(name))
     for value in values:
+        ifcond = None
+        if isinstance(value, tuple):
+            value, ifcond = value
+        ret += gen_if(ifcond)
         index = c_enum_const(name, value, prefix)
         ret += mcgen('''
     [%(index)s] = "%(value)s",
 ''',
                      index=index, value=value)
+        ret += gen_endif(ifcond)
 
     max_index = c_enum_const(name, '_MAX', prefix)
     ret += mcgen('''
@@ -1969,12 +1991,17 @@ typedef enum %(c_name)s {
 
     i = 0
     for value in enum_values:
+        ifcond = None
+        if isinstance(value, tuple):
+            value, ifcond = value
+        ret += gen_if(ifcond)
         ret += mcgen('''
     %(c_enum)s = %(i)d,
 ''',
                      c_enum=c_enum_const(name, value, prefix),
                      i=i)
         i += 1
+        ret += gen_endif(ifcond)
 
     ret += mcgen('''
 } %(c_name)s;
tests/Makefile.include +2 -0
index 0fc7088b2c..cb5daf0009 100644
@@ -413,6 +413,8 @@ qapi-schema += enum-bad-name.json
 qapi-schema += enum-bad-prefix.json
 qapi-schema += enum-clash-member.json
 qapi-schema += enum-dict-member.json
+qapi-schema += enum-dict-member-invalid.json
+qapi-schema += enum-if-invalid.json
 qapi-schema += enum-int-member.json
 qapi-schema += enum-member-case.json
 qapi-schema += enum-missing-data.json
tests/qapi-schema/enum-dict-member-invalid.err new file +1 -0
new file mode 100644
index 0000000000..d12cca0df3
@@ -0,0 +1 @@
+tests/qapi-schema/enum-dict-member-invalid.json:2: Dictionary member of enum 'MyEnum' must have a 'name' key
tests/qapi-schema/enum-dict-member-invalid.exit new file +1 -0
new file mode 100644
index 0000000000..d00491fd7e
@@ -0,0 +1 @@
+1
tests/qapi-schema/enum-dict-member-invalid.json new file +2 -0
new file mode 100644
index 0000000000..9cf8406867
@@ -0,0 +1,2 @@
+# we reject any enum member that is not a string or a dict with 'name'
+{ 'enum': 'MyEnum', 'data': [ { 'value': 'str' } ] }
tests/qapi-schema/enum-dict-member-invalid.out new file +0 -0
new file mode 100644
index 0000000000..e69de29bb2
tests/qapi-schema/enum-dict-member.err +0 -1
index 8ca146ea59..e69de29bb2 100644
@@ -1 +0,0 @@
-tests/qapi-schema/enum-dict-member.json:2: Member of enum 'MyEnum' requires a string name
tests/qapi-schema/enum-dict-member.exit +1 -1
index d00491fd7e..573541ac97 100644
@@ -1 +1 @@
-1
+0
tests/qapi-schema/enum-dict-member.json +1 -2
index 79672e0f09..b09a83061c 100644
@@ -1,2 +1 @@
-# we reject any enum member that is not a string
-{ 'enum': 'MyEnum', 'data': [ { 'value': 'str' } ] }
+{ 'enum': 'TestEnum', 'data': [ { 'name': 'foo' }, { 'name': 'bar' } ]}
tests/qapi-schema/enum-dict-member.out +4 -0
index e69de29bb2..cf8e3cce2b 100644
@@ -0,0 +1,4 @@
+enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool']
+    prefix QTYPE
+enum TestEnum ['foo', 'bar']
+object q_empty
tests/qapi-schema/enum-if-invalid.err new file +1 -0
new file mode 100644
index 0000000000..236b703e88
@@ -0,0 +1 @@
+tests/qapi-schema/enum-if-invalid.json:2: 'if' condition requires a string or a list of string
tests/qapi-schema/enum-if-invalid.exit new file +1 -0
new file mode 100644
index 0000000000..d00491fd7e
@@ -0,0 +1 @@
+1
tests/qapi-schema/enum-if-invalid.json new file +3 -0
new file mode 100644
index 0000000000..60bd0ef1d7
@@ -0,0 +1,3 @@
+# check invalid 'if' type
+{ 'enum': 'TestIfEnum', 'data':
+  [ 'foo', { 'name' : 'bar', 'if': { 'val': 'foo' } } ] }
tests/qapi-schema/enum-if-invalid.out new file +0 -0
new file mode 100644
index 0000000000..e69de29bb2
tests/qapi-schema/qapi-schema-test.json +3 -2
index dc2c444fc1..ad2b405d83 100644
@@ -194,7 +194,8 @@
 { 'struct': 'TestIfStruct', 'data': { 'foo': 'int' },
   'if': 'defined(TEST_IF_STRUCT)' }
 
-{ 'enum': 'TestIfEnum', 'data': [ 'foo', 'bar' ],
+{ 'enum': 'TestIfEnum', 'data':
+  [ 'foo', { 'name' : 'bar', 'if': 'defined(TEST_IF_ENUM_BAR)' } ],
   'if': 'defined(TEST_IF_ENUM)' }
 
 { 'union': 'TestIfUnion', 'data': { 'foo': 'TestStruct' },
@@ -203,7 +204,7 @@
 { 'alternate': 'TestIfAlternate', 'data': { 'foo': 'int', 'bar': 'TestStruct' },
   'if': 'defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)' }
 
-{ 'command': 'TestIfCmd', 'data': { 'foo': 'TestIfStruct' },
+{ 'command': 'TestIfCmd', 'data': { 'foo': 'TestIfStruct', 'bar': 'TestIfEnum' },
   'if': 'defined(TEST_IF_CMD) && defined(TEST_IF_STRUCT)' }
 
 { 'event': 'TestIfEvent', 'data': { 'foo': 'TestIfStruct' },
tests/qapi-schema/qapi-schema-test.out +2 -1
index fc5fd25f1b..211c367632 100644
@@ -60,7 +60,7 @@ alternate TestIfAlternate
 command TestIfCmd q_obj_TestIfCmd-arg -> None
    gen=True success_response=True boxed=False
     if defined(TEST_IF_CMD) && defined(TEST_IF_STRUCT)
-enum TestIfEnum ['foo', 'bar']
+enum TestIfEnum ['foo', ('bar', 'defined(TEST_IF_ENUM_BAR)')]
     if defined(TEST_IF_ENUM)
 event TestIfEvent q_obj_TestIfEvent-arg
    boxed=False
@@ -197,6 +197,7 @@ object q_obj_EVENT_D-arg
     member enum3: EnumOne optional=True
 object q_obj_TestIfCmd-arg
     member foo: TestIfStruct optional=False
+    member bar: TestIfEnum optional=False
     if defined(TEST_IF_CMD) && defined(TEST_IF_STRUCT)
 object q_obj_TestIfEvent-arg
     member foo: TestIfStruct optional=False
tests/qapi-schema/test-qapi.py +3 -1
index 17fd975812..70054848f0 100644
@@ -18,7 +18,9 @@ import sys
 
 class QAPISchemaTestVisitor(QAPISchemaVisitor):
     def visit_enum_type(self, name, info, values, prefix, ifcond):
-        print 'enum %s %s' % (name, values)
+        values = ', '.join(["'%s'" % v[0] if not v[1] else str(v)
+                            for v in values])
+        print 'enum %s [%s]' % (name, values)
         if prefix:
             print '    prefix %s' % prefix
         self._print_if(ifcond)
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#26 lore raw

PATCH 9/26 qapi: add 'if' condition on struct member

In reply to Marc-André Lureau
---
 scripts/qapi.py                           | 18 ++++++++++++++----
 scripts/qapi-introspect.py                |  2 ++
 scripts/qapi-types.py                     |  2 ++
 scripts/qapi-visit.py                     |  2 ++
 tests/Makefile.include                    |  2 ++
 tests/qapi-schema/qapi-schema-test.json   |  9 ++++++---
 tests/qapi-schema/qapi-schema-test.out    |  4 +++-
 tests/qapi-schema/struct-if-invalid.err   |  1 +
 tests/qapi-schema/struct-if-invalid.exit  |  1 +
 tests/qapi-schema/struct-if-invalid.json  |  3 +++
 tests/qapi-schema/struct-if-invalid.out   |  0
 tests/qapi-schema/struct-member-type.err  |  0
 tests/qapi-schema/struct-member-type.exit |  1 +
 tests/qapi-schema/struct-member-type.json |  1 +
 tests/qapi-schema/struct-member-type.out  |  5 +++++
 tests/qapi-schema/test-qapi.py            |  3 ++-
 16 files changed, 45 insertions(+), 9 deletions(-)
 create mode 100644 tests/qapi-schema/struct-if-invalid.err
 create mode 100644 tests/qapi-schema/struct-if-invalid.exit
 create mode 100644 tests/qapi-schema/struct-if-invalid.json
 create mode 100644 tests/qapi-schema/struct-if-invalid.out
 create mode 100644 tests/qapi-schema/struct-member-type.err
 create mode 100644 tests/qapi-schema/struct-member-type.exit
 create mode 100644 tests/qapi-schema/struct-member-type.json
 create mode 100644 tests/qapi-schema/struct-member-type.out
scripts/qapi.py +14 -4
index 93d07283e6..1eb40590fb 100644
@@ -678,7 +678,13 @@ def check_type(info, source, value, allow_array=False,
         return
 
     if not allow_dict:
-        raise QAPISemError(info, "%s should be a type name" % source)
+        if isinstance(value, dict) and 'type' in value:
+            check_type(info, source, value['type'], allow_array,
+                       allow_dict, allow_optional, allow_metas)
+            check_if(value, info)
+            return
+        else:
+            raise QAPISemError(info, "%s should be a type name" % source)
 
     if not isinstance(value, OrderedDict):
         raise QAPISemError(info,
@@ -1333,8 +1339,8 @@ class QAPISchemaMember(object):
 
 
 class QAPISchemaObjectTypeMember(QAPISchemaMember):
-    def __init__(self, name, typ, optional):
-        QAPISchemaMember.__init__(self, name)
+    def __init__(self, name, typ, optional, ifcond=None):
+        QAPISchemaMember.__init__(self, name, ifcond)
         assert isinstance(typ, str)
         assert isinstance(optional, bool)
         self._type_name = typ
@@ -1611,13 +1617,17 @@ class QAPISchema(object):
 
     def _make_member(self, name, typ, info):
         optional = False
+        ifcond = None
         if name.startswith('*'):
             name = name[1:]
             optional = True
+        if isinstance(typ, dict):
+            ifcond = typ.get('if')
+            typ = typ['type']
         if isinstance(typ, list):
             assert len(typ) == 1
             typ = self._make_array_type(typ[0], info)
-        return QAPISchemaObjectTypeMember(name, typ, optional)
+        return QAPISchemaObjectTypeMember(name, typ, optional, ifcond)
 
     def _make_members(self, data, info):
         return [self._make_member(key, value, info)
scripts/qapi-introspect.py +2 -0
index ecfb0f2752..98b320a79e 100644
@@ -130,6 +130,8 @@ const QLitObject %(c_name)s = %(c_string)s;
         ret = {'name': member.name, 'type': self._use_type(member.type)}
         if member.optional:
             ret['default'] = None
+        if member.ifcond:
+            ret = (ret, member.ifcond)
         return ret
 
     def _gen_variants(self, tag_name, variants):
scripts/qapi-types.py +2 -0
index d0d2eb917c..659fb1da86 100644
@@ -41,6 +41,7 @@ struct %(c_name)s {
 def gen_struct_members(members):
     ret = ''
     for memb in members:
+        ret += gen_if(memb.ifcond)
         if memb.optional:
             ret += mcgen('''
     bool has_%(c_name)s;
@@ -50,6 +51,7 @@ def gen_struct_members(members):
     %(c_type)s %(c_name)s;
 ''',
                      c_type=memb.type.c_type(), c_name=c_name(memb.name))
+        ret += gen_endif(memb.ifcond)
     return ret
 
 
scripts/qapi-visit.py +2 -0
index 335407d078..f400cef13c 100644
@@ -53,6 +53,7 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
                      c_type=base.c_name())
 
     for memb in members:
+        ret += gen_if(memb.ifcond)
         if memb.optional:
             ret += mcgen('''
     if (visit_optional(v, "%(name)s", &obj->has_%(c_name)s)) {
@@ -72,6 +73,7 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
             ret += mcgen('''
     }
 ''')
+        ret += gen_endif(memb.ifcond)
 
     if variants:
         ret += mcgen('''
tests/Makefile.include +2 -0
index cb5daf0009..816cc5c5e3 100644
@@ -489,7 +489,9 @@ qapi-schema += returns-whitelist.json
 qapi-schema += struct-base-clash-deep.json
 qapi-schema += struct-base-clash.json
 qapi-schema += struct-data-invalid.json
+qapi-schema += struct-if-invalid.json
 qapi-schema += struct-member-invalid.json
+qapi-schema += struct-member-type.json
 qapi-schema += trailing-comma-list.json
 qapi-schema += trailing-comma-object.json
 qapi-schema += type-bypass-bad-gen.json
tests/qapi-schema/qapi-schema-test.json +6 -3
index ad2b405d83..bb515280d2 100644
@@ -191,7 +191,8 @@
 
 # test 'if' condition handling
 
-{ 'struct': 'TestIfStruct', 'data': { 'foo': 'int' },
+{ 'struct': 'TestIfStruct', 'data':
+  { 'foo': 'int', 'bar': { 'type': 'int', 'if': 'defined(TEST_IF_STRUCT_BAR)'} },
   'if': 'defined(TEST_IF_STRUCT)' }
 
 { 'enum': 'TestIfEnum', 'data':
@@ -204,8 +205,10 @@
 { 'alternate': 'TestIfAlternate', 'data': { 'foo': 'int', 'bar': 'TestStruct' },
   'if': 'defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)' }
 
-{ 'command': 'TestIfCmd', 'data': { 'foo': 'TestIfStruct', 'bar': 'TestIfEnum' },
+{ 'command': 'TestIfCmd', 'data':
+  { 'foo': 'TestIfStruct', 'bar': { 'type': 'TestIfEnum', 'if': 'TEST_IF_CMD_BAR' } },
   'if': 'defined(TEST_IF_CMD) && defined(TEST_IF_STRUCT)' }
 
-{ 'event': 'TestIfEvent', 'data': { 'foo': 'TestIfStruct' },
+{ 'event': 'TestIfEvent', 'data':
+  { 'foo': 'TestIfStruct', 'bar': { 'type': 'TestIfEnum', 'if': 'TEST_IF_EVT_BAR' } },
   'if': 'defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)' }
tests/qapi-schema/qapi-schema-test.out +3 -1
index 211c367632..b17c32a45f 100644
@@ -67,6 +67,7 @@ event TestIfEvent q_obj_TestIfEvent-arg
     if defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)
 object TestIfStruct
     member foo: int optional=False
+    member bar: int optional=False if=defined(TEST_IF_STRUCT_BAR)
     if defined(TEST_IF_STRUCT)
 object TestIfUnion
     member type: TestIfUnionKind optional=False
@@ -197,10 +198,11 @@ object q_obj_EVENT_D-arg
     member enum3: EnumOne optional=True
 object q_obj_TestIfCmd-arg
     member foo: TestIfStruct optional=False
-    member bar: TestIfEnum optional=False
+    member bar: TestIfEnum optional=False if=TEST_IF_CMD_BAR
     if defined(TEST_IF_CMD) && defined(TEST_IF_STRUCT)
 object q_obj_TestIfEvent-arg
     member foo: TestIfStruct optional=False
+    member bar: TestIfEnum optional=False if=TEST_IF_EVT_BAR
     if defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)
 object q_obj_TestStruct-wrapper
     member data: TestStruct optional=False
tests/qapi-schema/struct-if-invalid.err new file +1 -0
new file mode 100644
index 0000000000..42245262a9
@@ -0,0 +1 @@
+tests/qapi-schema/struct-if-invalid.json:2: Member 'bar' of 'data' for struct 'TestIfStruct' should be a type name
tests/qapi-schema/struct-if-invalid.exit new file +1 -0
new file mode 100644
index 0000000000..d00491fd7e
@@ -0,0 +1 @@
+1
tests/qapi-schema/struct-if-invalid.json new file +3 -0
new file mode 100644
index 0000000000..466cdb61e1
@@ -0,0 +1,3 @@
+# check missing 'type'
+{ 'struct': 'TestIfStruct', 'data':
+  { 'foo': 'int', 'bar': { 'if': 'defined(TEST_IF_STRUCT_BAR)' } } }
tests/qapi-schema/struct-if-invalid.out new file +0 -0
new file mode 100644
index 0000000000..e69de29bb2
tests/qapi-schema/struct-member-type.err new file +0 -0
new file mode 100644
index 0000000000..e69de29bb2
tests/qapi-schema/struct-member-type.exit new file +1 -0
new file mode 100644
index 0000000000..573541ac97
@@ -0,0 +1 @@
+0
tests/qapi-schema/struct-member-type.json new file +1 -0
new file mode 100644
index 0000000000..07de38fa4a
@@ -0,0 +1 @@
+{ 'struct': 'foo', 'data': { 'a': { 'type': 'str' } } }
tests/qapi-schema/struct-member-type.out new file +5 -0
new file mode 100644
index 0000000000..2ea68909a9
@@ -0,0 +1,5 @@
+enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool']
+    prefix QTYPE
+object foo
+    member a: str optional=False
+object q_empty
tests/qapi-schema/test-qapi.py +2 -1
index 70054848f0..5d2f67a1d3 100644
@@ -31,7 +31,8 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor):
             print '    base %s' % base.name
         for m in members:
             print '    member %s: %s optional=%s' % \
-                (m.name, m.type.name, m.optional)
+                (m.name, m.type.name, m.optional) + \
+                (' if=%s' % m.ifcond if m.ifcond else '')
         self._print_variants(variants)
         self._print_if(ifcond)
 
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#27 lore raw

PATCH 10/26 qapi: add 'if' condition on union variant

In reply to Marc-André Lureau
---
 scripts/qapi.py                         | 14 +++++++++-----
 scripts/qapi-introspect.py              |  3 ++-
 scripts/qapi-types.py                   |  2 ++
 scripts/qapi-visit.py                   |  4 ++++
 tests/qapi-schema/qapi-schema-test.json |  6 +++++-
 tests/qapi-schema/qapi-schema-test.out  |  9 ++++++++-
 tests/qapi-schema/test-qapi.py          |  3 ++-
 7 files changed, 32 insertions(+), 9 deletions(-)
scripts/qapi.py +9 -5
index 1eb40590fb..333a5c0d1e 100644
@@ -1398,8 +1398,8 @@ class QAPISchemaObjectTypeVariants(object):
 class QAPISchemaObjectTypeVariant(QAPISchemaObjectTypeMember):
     role = 'branch'
 
-    def __init__(self, name, typ):
-        QAPISchemaObjectTypeMember.__init__(self, name, typ, False)
+    def __init__(self, name, typ, ifcond=None):
+        QAPISchemaObjectTypeMember.__init__(self, name, typ, False, ifcond)
 
 
 class QAPISchemaAlternateType(QAPISchemaType):
@@ -1646,6 +1646,10 @@ class QAPISchema(object):
         return QAPISchemaObjectTypeVariant(case, typ)
 
     def _make_simple_variant(self, case, typ, info):
+        ifcond = None
+        if isinstance(typ, dict):
+            ifcond = typ.get('if')
+            typ = typ['type']
         if isinstance(typ, list):
             assert len(typ) == 1
             typ = self._make_array_type(typ[0], info)
@@ -1653,7 +1657,7 @@ class QAPISchema(object):
         typ = self._make_implicit_object_type(
             typ, info, None, 'wrapper',
             [self._make_member('data', typ, info)], type_entity.ifcond)
-        return QAPISchemaObjectTypeVariant(case, typ)
+        return QAPISchemaObjectTypeVariant(case, typ, ifcond)
 
     def _def_union_type(self, expr, info, doc):
         name = expr['union']
@@ -1672,8 +1676,8 @@ class QAPISchema(object):
         else:
             variants = [self._make_simple_variant(key, value, info)
                         for (key, value) in data.iteritems()]
-            typ = self._make_implicit_enum_type(name, info,
-                                                [v.name for v in variants],
+            values = [{'name': v.name, 'if': v.ifcond} for v in variants]
+            typ = self._make_implicit_enum_type(name, info, values,
                                                 expr.get('if'))
             tag_member = QAPISchemaObjectTypeMember('type', typ, False)
             members = [tag_member]
scripts/qapi-introspect.py +2 -1
index 98b320a79e..a867ea5de1 100644
@@ -139,7 +139,8 @@ const QLitObject %(c_name)s = %(c_string)s;
                 'variants': [self._gen_variant(v) for v in variants]}
 
     def _gen_variant(self, variant):
-        return {'case': variant.name, 'type': self._use_type(variant.type)}
+        return ({'case': variant.name, 'type': self._use_type(variant.type)},
+                variant.ifcond)
 
     def visit_builtin_type(self, name, info, json_type):
         self._gen_qlit(name, 'builtin', {'json-type': json_type}, None)
scripts/qapi-types.py +2 -0
index 659fb1da86..d6c8feb55a 100644
@@ -128,11 +128,13 @@ def gen_variants(variants):
                 c_name=c_name(variants.tag_member.name))
 
     for var in variants.variants:
+        ret += gen_if(var.ifcond)
         ret += mcgen('''
         %(c_type)s %(c_name)s;
 ''',
                      c_type=var.type.c_unboxed_type(),
                      c_name=c_name(var.name))
+        ret += gen_endif(var.ifcond)
 
     ret += mcgen('''
     } u;
scripts/qapi-visit.py +4 -0
index f400cef13c..0f5cae33e4 100644
@@ -82,6 +82,7 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
                      c_name=c_name(variants.tag_member.name))
 
         for var in variants.variants:
+            ret += gen_if(var.ifcond)
             ret += mcgen('''
     case %(case)s:
         visit_type_%(c_type)s_members(v, &obj->u.%(c_name)s, &err);
@@ -92,6 +93,7 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
                                            variants.tag_member.type.prefix),
                          c_type=var.type.c_name(), c_name=c_name(var.name))
 
+            ret += gen_endif(var.ifcond)
         ret += mcgen('''
     default:
         abort();
@@ -182,6 +184,7 @@ void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error
                  c_name=c_name(name))
 
     for var in variants.variants:
+        ret += gen_if(var.ifcond)
         ret += mcgen('''
     case %(case)s:
 ''',
@@ -209,6 +212,7 @@ void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error
         ret += mcgen('''
         break;
 ''')
+        ret += gen_endif(var.ifcond)
 
     ret += mcgen('''
     case QTYPE_NONE:
tests/qapi-schema/qapi-schema-test.json +5 -1
index bb515280d2..01f7db58dc 100644
@@ -199,9 +199,13 @@
   [ 'foo', { 'name' : 'bar', 'if': 'defined(TEST_IF_ENUM_BAR)' } ],
   'if': 'defined(TEST_IF_ENUM)' }
 
-{ 'union': 'TestIfUnion', 'data': { 'foo': 'TestStruct' },
+{ 'union': 'TestIfUnion', 'data':
+  { 'foo': 'TestStruct', 'union_bar': { 'type': 'str', 'if': 'defined(TEST_IF_UNION_BAR)'} },
   'if': 'defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)' }
 
+{ 'command': 'TestIfUnionCmd', 'data': { 'union_cmd_arg': 'TestIfUnion' },
+  'if': 'defined(TEST_IF_UNION)' }
+
 { 'alternate': 'TestIfAlternate', 'data': { 'foo': 'int', 'bar': 'TestStruct' },
   'if': 'defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)' }
 
tests/qapi-schema/qapi-schema-test.out +8 -1
index b17c32a45f..7908eed6fc 100644
@@ -73,8 +73,12 @@ object TestIfUnion
     member type: TestIfUnionKind optional=False
     tag type
     case foo: q_obj_TestStruct-wrapper
+    case union_bar: q_obj_str-wrapper if=defined(TEST_IF_UNION_BAR)
     if defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)
-enum TestIfUnionKind ['foo']
+command TestIfUnionCmd q_obj_TestIfUnionCmd-arg -> None
+   gen=True success_response=True boxed=False
+    if defined(TEST_IF_UNION)
+enum TestIfUnionKind ['foo', ('union_bar', 'defined(TEST_IF_UNION_BAR)')]
     if defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)
 object TestStruct
     member integer: int optional=False
@@ -204,6 +208,9 @@ object q_obj_TestIfEvent-arg
     member foo: TestIfStruct optional=False
     member bar: TestIfEnum optional=False if=TEST_IF_EVT_BAR
     if defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)
+object q_obj_TestIfUnionCmd-arg
+    member union_cmd_arg: TestIfUnion optional=False
+    if defined(TEST_IF_UNION)
 object q_obj_TestStruct-wrapper
     member data: TestStruct optional=False
 object q_obj_UserDefFlatUnion2-base
tests/qapi-schema/test-qapi.py +2 -1
index 5d2f67a1d3..b360d44df1 100644
@@ -59,7 +59,8 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor):
         if variants:
             print '    tag %s' % variants.tag_member.name
             for v in variants.variants:
-                print '    case %s: %s' % (v.name, v.type.name)
+                print '    case %s: %s' % (v.name, v.type.name) + \
+                    (' if=%s' % v.ifcond if v.ifcond else '')
 
     @staticmethod
     def _print_if(ifcond):
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#28 lore raw

PATCH 11/26 qapi: add 'if' condition on alternate variant

In reply to Marc-André Lureau
---
 scripts/qapi.py                               | 8 +++++++-
 scripts/qapi-introspect.py                    | 2 +-
 tests/Makefile.include                        | 1 +
 tests/qapi-schema/alternate-dict-invalid.err  | 1 +
 tests/qapi-schema/alternate-dict-invalid.exit | 1 +
 tests/qapi-schema/alternate-dict-invalid.json | 4 ++++
 tests/qapi-schema/alternate-dict-invalid.out  | 0
 tests/qapi-schema/qapi-schema-test.json       | 6 +++++-
 tests/qapi-schema/qapi-schema-test.out        | 8 +++++++-
 9 files changed, 27 insertions(+), 4 deletions(-)
 create mode 100644 tests/qapi-schema/alternate-dict-invalid.err
 create mode 100644 tests/qapi-schema/alternate-dict-invalid.exit
 create mode 100644 tests/qapi-schema/alternate-dict-invalid.json
 create mode 100644 tests/qapi-schema/alternate-dict-invalid.out
scripts/qapi.py +7 -1
index 333a5c0d1e..7f0d56fb01 100644
@@ -829,6 +829,8 @@ def check_alternate(expr, info):
         check_type(info, "Member '%s' of alternate '%s'" % (key, name),
                    value,
                    allow_metas=['built-in', 'union', 'struct', 'enum'])
+        if isinstance(value, dict):
+            value = value['type']
         qtype = find_alternate_member_qtype(value)
         if not qtype:
             raise QAPISemError(info, "Alternate '%s' member '%s' cannot use "
@@ -1643,7 +1645,11 @@ class QAPISchema(object):
                          expr.get('if'))
 
     def _make_variant(self, case, typ):
-        return QAPISchemaObjectTypeVariant(case, typ)
+        ifcond = None
+        if isinstance(typ, dict):
+            ifcond = typ.get('if')
+            typ = typ['type']
+        return QAPISchemaObjectTypeVariant(case, typ, ifcond)
 
     def _make_simple_variant(self, case, typ, info):
         ifcond = None
scripts/qapi-introspect.py +1 -1
index a867ea5de1..cac5a6a392 100644
@@ -162,7 +162,7 @@ const QLitObject %(c_name)s = %(c_string)s;
 
     def visit_alternate_type(self, name, info, variants, ifcond):
         self._gen_qlit(name, 'alternate',
-                       {'members': [{'type': self._use_type(m.type)}
+                       {'members': [({'type': self._use_type(m.type)}, m.ifcond)
                                     for m in variants.variants]}, ifcond)
 
     def visit_command(self, name, info, arg_type, ret_type,
tests/Makefile.include +1 -0
index 816cc5c5e3..9462deb02b 100644
@@ -351,6 +351,7 @@ qapi-schema += alternate-conflict-dict.json
 qapi-schema += alternate-conflict-enum-bool.json
 qapi-schema += alternate-conflict-enum-int.json
 qapi-schema += alternate-conflict-string.json
+qapi-schema += alternate-dict-invalid.json
 qapi-schema += alternate-empty.json
 qapi-schema += alternate-nested.json
 qapi-schema += alternate-unknown.json
tests/qapi-schema/alternate-dict-invalid.err new file +1 -0
new file mode 100644
index 0000000000..707c40f0f6
@@ -0,0 +1 @@
+tests/qapi-schema/alternate-dict-invalid.json:2: Member 'two' of alternate 'Alt' should be a type name
tests/qapi-schema/alternate-dict-invalid.exit new file +1 -0
new file mode 100644
index 0000000000..d00491fd7e
@@ -0,0 +1 @@
+1
tests/qapi-schema/alternate-dict-invalid.json new file +4 -0
new file mode 100644
index 0000000000..45f2c8ebef
@@ -0,0 +1,4 @@
+# invalid field dictionnary, missing type
+{ 'alternate': 'Alt',
+  'data': { 'one': 'str',
+            'two': { 'if': 'foo' } } }
tests/qapi-schema/alternate-dict-invalid.out new file +0 -0
new file mode 100644
index 0000000000..e69de29bb2
tests/qapi-schema/qapi-schema-test.json +5 -1
index 01f7db58dc..46a4d1c3e5 100644
@@ -206,9 +206,13 @@
 { 'command': 'TestIfUnionCmd', 'data': { 'union_cmd_arg': 'TestIfUnion' },
   'if': 'defined(TEST_IF_UNION)' }
 
-{ 'alternate': 'TestIfAlternate', 'data': { 'foo': 'int', 'bar': 'TestStruct' },
+{ 'alternate': 'TestIfAlternate', 'data':
+  { 'foo': 'int', 'alt_bar': { 'type': 'TestStruct', 'if': 'defined(TEST_IF_ALT_BAR)'} },
   'if': 'defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)' }
 
+{ 'command': 'TestIfAlternateCmd', 'data': { 'alt_cmd_arg': 'TestIfAlternate' },
+  'if': 'defined(TEST_IF_ALT)' }
+
 { 'command': 'TestIfCmd', 'data':
   { 'foo': 'TestIfStruct', 'bar': { 'type': 'TestIfEnum', 'if': 'TEST_IF_CMD_BAR' } },
   'if': 'defined(TEST_IF_CMD) && defined(TEST_IF_STRUCT)' }
tests/qapi-schema/qapi-schema-test.out +7 -1
index 7908eed6fc..a51c6ac15c 100644
@@ -55,8 +55,11 @@ enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool']
 alternate TestIfAlternate
     tag type
     case foo: int
-    case bar: TestStruct
+    case alt_bar: TestStruct if=defined(TEST_IF_ALT_BAR)
     if defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)
+command TestIfAlternateCmd q_obj_TestIfAlternateCmd-arg -> None
+   gen=True success_response=True boxed=False
+    if defined(TEST_IF_ALT)
 command TestIfCmd q_obj_TestIfCmd-arg -> None
    gen=True success_response=True boxed=False
     if defined(TEST_IF_CMD) && defined(TEST_IF_STRUCT)
@@ -200,6 +203,9 @@ object q_obj_EVENT_D-arg
     member b: str optional=False
     member c: str optional=True
     member enum3: EnumOne optional=True
+object q_obj_TestIfAlternateCmd-arg
+    member alt_cmd_arg: TestIfAlternate optional=False
+    if defined(TEST_IF_ALT)
 object q_obj_TestIfCmd-arg
     member foo: TestIfStruct optional=False
     member bar: TestIfEnum optional=False if=TEST_IF_CMD_BAR
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#29 lore raw

PATCH 12/26 qapi2texi: add 'If:' section to generated documentation

In reply to Marc-André Lureau

The documentation is generated only once, and doesn't know C pre-conditions.

---
 scripts/qapi2texi.py | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)
scripts/qapi2texi.py +13 -11
index 5aa2f48bfd..bfb8cd7c52 100755
@@ -132,7 +132,6 @@ def texi_enum_value(value):
     """Format a table of members item for an enumeration value"""
     return '@item @code{%s}\n' % value.name
 
-
 def texi_member(member, suffix=''):
     """Format a table of members item for an object type member"""
     typ = member.type.doc_type()
@@ -175,7 +174,7 @@ def texi_members(doc, what, base, variants, member_func):
     return '\n@b{%s:}\n@table @asis\n%s@end table\n' % (what, items)
 
 
-def texi_sections(doc):
+def texi_sections(doc, ifcond):
     """Format additional sections following arguments"""
     body = ''
     for section in doc.sections:
@@ -189,14 +188,16 @@ def texi_sections(doc):
             body += '\n\n@b{%s:}\n' % name
 
         body += func(doc)
+    if ifcond:
+        body += '\n\n@b{If:} @code{%s}' % ifcond
     return body
 
 
-def texi_entity(doc, what, base=None, variants=None,
+def texi_entity(doc, what, ifcond, base=None, variants=None,
                 member_func=texi_member):
     return (texi_body(doc)
             + texi_members(doc, what, base, variants, member_func)
-            + texi_sections(doc))
+            + texi_sections(doc, ifcond))
 
 
 class QAPISchemaGenDocVisitor(qapi.QAPISchemaVisitor):
@@ -213,7 +214,7 @@ class QAPISchemaGenDocVisitor(qapi.QAPISchemaVisitor):
             self.out += '\n'
         self.out += TYPE_FMT(type='Enum',
                              name=doc.symbol,
-                             body=texi_entity(doc, 'Values',
+                             body=texi_entity(doc, 'Values', ifcond,
                                               member_func=texi_enum_value))
 
     def visit_object_type(self, name, info, base, members, variants, ifcond):
@@ -224,7 +225,8 @@ class QAPISchemaGenDocVisitor(qapi.QAPISchemaVisitor):
             self.out += '\n'
         self.out += TYPE_FMT(type='Object',
                              name=doc.symbol,
-                             body=texi_entity(doc, 'Members', base, variants))
+                             body=texi_entity(doc, 'Members', ifcond,
+                                              base, variants))
 
     def visit_alternate_type(self, name, info, variants, ifcond):
         doc = self.cur_doc
@@ -232,7 +234,7 @@ class QAPISchemaGenDocVisitor(qapi.QAPISchemaVisitor):
             self.out += '\n'
         self.out += TYPE_FMT(type='Alternate',
                              name=doc.symbol,
-                             body=texi_entity(doc, 'Members'))
+                             body=texi_entity(doc, 'Members', ifcond))
 
     def visit_command(self, name, info, arg_type, ret_type,
                       gen, success_response, boxed, ifcond):
@@ -242,9 +244,9 @@ class QAPISchemaGenDocVisitor(qapi.QAPISchemaVisitor):
         if boxed:
             body = texi_body(doc)
             body += '\n@b{Arguments:} the members of @code{%s}' % arg_type.name
-            body += texi_sections(doc)
+            body += texi_sections(doc, ifcond)
         else:
-            body = texi_entity(doc, 'Arguments')
+            body = texi_entity(doc, 'Arguments', ifcond)
         self.out += MSG_FMT(type='Command',
                             name=doc.symbol,
                             body=body)
@@ -255,7 +257,7 @@ class QAPISchemaGenDocVisitor(qapi.QAPISchemaVisitor):
             self.out += '\n'
         self.out += MSG_FMT(type='Event',
                             name=doc.symbol,
-                            body=texi_entity(doc, 'Arguments'))
+                            body=texi_entity(doc, 'Arguments', ifcond))
 
     def symbol(self, doc, entity):
         self.cur_doc = doc
@@ -266,7 +268,7 @@ class QAPISchemaGenDocVisitor(qapi.QAPISchemaVisitor):
         assert not doc.args
         if self.out:
             self.out += '\n'
-        self.out += texi_body(doc) + texi_sections(doc)
+        self.out += texi_body(doc) + texi_sections(doc, None)
 
 
 def texi_schema(schema):
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#30 lore raw

PATCH 13/26 qapi2texi: add 'If:' condition to enum values

In reply to Marc-André Lureau
---
 scripts/qapi2texi.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/qapi2texi.py +3 -1
index bfb8cd7c52..340a55c30d 100755
@@ -130,7 +130,9 @@ def texi_body(doc):
 
 def texi_enum_value(value):
     """Format a table of members item for an enumeration value"""
-    return '@item @code{%s}\n' % value.name
+    return '@item @code{%s}%s\n' % (
+        value.name,
+        '\n@b{If:} @code{%s}\n' % value.ifcond if value.ifcond else '')
 
 def texi_member(member, suffix=''):
     """Format a table of members item for an object type member"""
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#31 lore raw

PATCH 14/26 qapi2texi: add 'If:' condition to struct members

In reply to Marc-André Lureau
---
 scripts/qapi2texi.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
scripts/qapi2texi.py +2 -1
index 340a55c30d..b2c9a9f335 100755
@@ -137,10 +137,11 @@ def texi_enum_value(value):
 def texi_member(member, suffix=''):
     """Format a table of members item for an object type member"""
     typ = member.type.doc_type()
-    return '@item @code{%s%s}%s%s\n' % (
+    return '@item @code{%s%s}%s%s%s\n' % (
         member.name,
         ': %s' % typ if typ else '',
         ' (optional)' if member.optional else '',
+        '\n@b{If:} @code{%s}\n' % member.ifcond if member.ifcond else '',
         suffix)
 
 
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#32 lore raw

PATCH 15/26 qapi2texi: add condition to variants

In reply to Marc-André Lureau
---
 scripts/qapi2texi.py | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
scripts/qapi2texi.py +3 -2
index b2c9a9f335..502f1e7c6a 100755
@@ -163,8 +163,9 @@ def texi_members(doc, what, base, variants, member_func):
         items += '@item The members of @code{%s}\n' % base.doc_type()
     if variants:
         for v in variants.variants:
-            when = ' when @code{%s} is @t{"%s"}' % (
-                variants.tag_member.name, v.name)
+            when = ' when @code{%s} is @t{"%s"}%s' % (
+                variants.tag_member.name, v.name,
+                ' (and @code{%s})' % v.ifcond if v.ifcond else '')
             if v.type.is_implicit():
                 assert not v.type.base and not v.type.variants
                 for m in v.type.local_members:
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#33 lore raw

PATCH 16/26 qapi: add conditions to VNC type/commands/events on the schema

In reply to Marc-André Lureau

Add #if defined(CONFIG_VNC) in generated code, and adjust the qmp/hmp code accordingly.

---
 qapi-schema.json | 34 ++++++++++++++++++++++------------
 qapi/event.json  |  9 ++++++---
 hmp.c            | 14 +++++++++++++-
 qmp.c            | 30 ++++--------------------------
 4 files changed, 45 insertions(+), 42 deletions(-)
qapi-schema.json +22 -12
index 9c6c3e1a53..829c66f9eb 100644
@@ -1660,7 +1660,8 @@
   'data': { 'host': 'str',
             'service': 'str',
             'family': 'NetworkAddressFamily',
-            'websocket': 'bool' } }
+            'websocket': 'bool' },
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @VncServerInfo:
@@ -1674,7 +1675,8 @@
 ##
 { 'struct': 'VncServerInfo',
   'base': 'VncBasicInfo',
-  'data': { '*auth': 'str' } }
+  'data': { '*auth': 'str' },
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @VncClientInfo:
@@ -1691,7 +1693,8 @@
 ##
 { 'struct': 'VncClientInfo',
   'base': 'VncBasicInfo',
-  'data': { '*x509_dname': 'str', '*sasl_username': 'str' } }
+  'data': { '*x509_dname': 'str', '*sasl_username': 'str' },
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @VncInfo:
@@ -1732,7 +1735,8 @@
 { 'struct': 'VncInfo',
   'data': {'enabled': 'bool', '*host': 'str',
            '*family': 'NetworkAddressFamily',
-           '*service': 'str', '*auth': 'str', '*clients': ['VncClientInfo']} }
+           '*service': 'str', '*auth': 'str', '*clients': ['VncClientInfo']},
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @VncPrimaryAuth:
@@ -1743,7 +1747,8 @@
 ##
 { 'enum': 'VncPrimaryAuth',
   'data': [ 'none', 'vnc', 'ra2', 'ra2ne', 'tight', 'ultra',
-            'tls', 'vencrypt', 'sasl' ] }
+            'tls', 'vencrypt', 'sasl' ],
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @VncVencryptSubAuth:
@@ -1757,7 +1762,8 @@
             'tls-none',  'x509-none',
             'tls-vnc',   'x509-vnc',
             'tls-plain', 'x509-plain',
-            'tls-sasl',  'x509-sasl' ] }
+            'tls-sasl',  'x509-sasl' ],
+  'if': 'defined(CONFIG_VNC)' }
 
 
 ##
@@ -1775,7 +1781,8 @@
 { 'struct': 'VncServerInfo2',
   'base': 'VncBasicInfo',
   'data': { 'auth'      : 'VncPrimaryAuth',
-            '*vencrypt' : 'VncVencryptSubAuth' } }
+            '*vencrypt' : 'VncVencryptSubAuth' },
+  'if': 'defined(CONFIG_VNC)' }
 
 
 ##
@@ -1808,7 +1815,8 @@
             'clients'   : ['VncClientInfo'],
             'auth'      : 'VncPrimaryAuth',
             '*vencrypt' : 'VncVencryptSubAuth',
-            '*display'  : 'str' } }
+            '*display'  : 'str' },
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @query-vnc:
@@ -1839,7 +1847,8 @@
 #    }
 #
 ##
-{ 'command': 'query-vnc', 'returns': 'VncInfo' }
+{ 'command': 'query-vnc', 'returns': 'VncInfo',
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @query-vnc-servers:
@@ -1850,7 +1859,8 @@
 #
 # Since: 2.3
 ##
-{ 'command': 'query-vnc-servers', 'returns': ['VncInfo2'] }
+{ 'command': 'query-vnc-servers', 'returns': ['VncInfo2'],
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @SpiceBasicInfo:
@@ -3077,8 +3087,8 @@
 # Notes:  An empty password in this command will set the password to the empty
 #         string.  Existing clients are unaffected by executing this command.
 ##
-{ 'command': 'change-vnc-password', 'data': {'password': 'str'} }
-
+{ 'command': 'change-vnc-password', 'data': {'password': 'str'},
+  'if': 'defined(CONFIG_VNC)' }
 ##
 # @change:
 #
qapi/event.json +6 -3
index 6d22b025cc..c8b8e9f384 100644
@@ -263,7 +263,8 @@
 ##
 { 'event': 'VNC_CONNECTED',
   'data': { 'server': 'VncServerInfo',
-            'client': 'VncBasicInfo' } }
+            'client': 'VncBasicInfo' },
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @VNC_INITIALIZED:
@@ -290,7 +291,8 @@
 ##
 { 'event': 'VNC_INITIALIZED',
   'data': { 'server': 'VncServerInfo',
-            'client': 'VncClientInfo' } }
+            'client': 'VncClientInfo' },
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @VNC_DISCONNECTED:
@@ -316,7 +318,8 @@
 ##
 { 'event': 'VNC_DISCONNECTED',
   'data': { 'server': 'VncServerInfo',
-            'client': 'VncClientInfo' } }
+            'client': 'VncClientInfo' },
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @SPICE_CONNECTED:
hmp.c +13 -1
index fd80dce758..9454c634bd 100644
@@ -605,6 +605,7 @@ void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
     qapi_free_BlockStatsList(stats_list);
 }
 
+#ifdef CONFIG_VNC
 /* Helper for hmp_info_vnc_clients, _servers */
 static void hmp_info_VncBasicInfo(Monitor *mon, VncBasicInfo *info,
                                   const char *name)
@@ -692,6 +693,12 @@ void hmp_info_vnc(Monitor *mon, const QDict *qdict)
     qapi_free_VncInfo2List(info2l);
 
 }
+#else
+void hmp_info_vnc(Monitor *mon, const QDict *qdict)
+{
+    warn_report("VNC support is disabled");
+}
+#endif
 
 #ifdef CONFIG_SPICE
 void hmp_info_spice(Monitor *mon, const QDict *qdict)
@@ -1708,12 +1715,14 @@ void hmp_eject(Monitor *mon, const QDict *qdict)
     hmp_handle_error(mon, &err);
 }
 
+#ifdef CONFIG_VNC
 static void hmp_change_read_arg(void *opaque, const char *password,
                                 void *readline_opaque)
 {
     qmp_change_vnc_password(password, NULL);
     monitor_read_command(opaque, 1);
 }
+#endif
 
 void hmp_change(Monitor *mon, const QDict *qdict)
 {
@@ -1724,6 +1733,7 @@ void hmp_change(Monitor *mon, const QDict *qdict)
     BlockdevChangeReadOnlyMode read_only_mode = 0;
     Error *err = NULL;
 
+#ifdef CONFIG_VNC
     if (strcmp(device, "vnc") == 0) {
         if (read_only) {
             monitor_printf(mon,
@@ -1738,7 +1748,9 @@ void hmp_change(Monitor *mon, const QDict *qdict)
             }
         }
         qmp_change("vnc", target, !!arg, arg, &err);
-    } else {
+    } else
+#endif
+    {
         if (read_only) {
             read_only_mode =
                 qapi_enum_parse(BlockdevChangeReadOnlyMode_lookup,
qmp.c +4 -26
index b86201e349..2c90dacb56 100644
@@ -130,22 +130,6 @@ void qmp_cpu_add(int64_t id, Error **errp)
     }
 }
 
-#ifndef CONFIG_VNC
-/* If VNC support is enabled, the "true" query-vnc command is
-   defined in the VNC subsystem */
-VncInfo *qmp_query_vnc(Error **errp)
-{
-    error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
-    return NULL;
-};
-
-VncInfo2List *qmp_query_vnc_servers(Error **errp)
-{
-    error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
-    return NULL;
-};
-#endif
-
 #ifndef CONFIG_SPICE
 /*
  * qmp-commands.hx ensures that QMP command query-spice exists only
@@ -403,23 +387,17 @@ static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
         qmp_change_vnc_listen(target, errp);
     }
 }
-#else
-void qmp_change_vnc_password(const char *password, Error **errp)
-{
-    error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
-}
-static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
-                           Error **errp)
-{
-    error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
-}
 #endif /* !CONFIG_VNC */
 
 void qmp_change(const char *device, const char *target,
                 bool has_arg, const char *arg, Error **errp)
 {
     if (strcmp(device, "vnc") == 0) {
+#ifdef CONFIG_VNC
         qmp_change_vnc(target, has_arg, arg, errp);
+#else
+        error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
+#endif
     } else {
         qmp_blockdev_change_medium(true, device, false, NULL, target,
                                    has_arg, arg, false, 0, errp);
Signature · 1 line
2.14.0.rc0.1.g40ca67566
Acked-by on v1 16/26
#34 lore raw

Re: [Qemu-devel] [PATCH 16/26] qapi: add conditions to VNC type/commands/events on the schema

Quoted 18 lines · * Marc-André Lureau ([email protected]) wrote:

Add #if defined(CONFIG_VNC) in generated code, and adjust the qmp/hmp code accordingly.

hmp.c +1 -0
index fd80dce758..9454c634bd 100644
@@ -605,6 +605,7 @@ void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
     qapi_free_BlockStatsList(stats_list);
 }
 
+#ifdef CONFIG_VNC
 /* Helper for hmp_info_vnc_clients, _servers */
 static void hmp_info_VncBasicInfo(Monitor *mon, VncBasicInfo *info,
                                   const char *name)

hmp.c void hmp_info_vnc(Monitor *mon, const QDict *qdict) · line 701

Quoted 9 lines
hmp.c +6 -0
@@ -692,6 +693,12 @@ void hmp_info_vnc(Monitor *mon, const QDict *qdict)
     qapi_free_VncInfo2List(info2l);
 
 }
+#else
+void hmp_info_vnc(Monitor *mon, const QDict *qdict)
+{
+    warn_report("VNC support is disabled");
+}
+#endif

I'm OK with this, so

although you might just be able to add a #ifdef in hmp-commands-info.hx and avoid the is disabled function, or you might find that with the QMP returning an error the HMP just passes that error on.

Dave

Quoted 94 lines
hmp.c +6 -1
 #ifdef CONFIG_SPICE
 void hmp_info_spice(Monitor *mon, const QDict *qdict)
@@ -1708,12 +1715,14 @@ void hmp_eject(Monitor *mon, const QDict *qdict)
     hmp_handle_error(mon, &err);
 }
 
+#ifdef CONFIG_VNC
 static void hmp_change_read_arg(void *opaque, const char *password,
                                 void *readline_opaque)
 {
     qmp_change_vnc_password(password, NULL);
     monitor_read_command(opaque, 1);
 }
+#endif
 
 void hmp_change(Monitor *mon, const QDict *qdict)
 {
@@ -1724,6 +1733,7 @@ void hmp_change(Monitor *mon, const QDict *qdict)
     BlockdevChangeReadOnlyMode read_only_mode = 0;
     Error *err = NULL;
 
+#ifdef CONFIG_VNC
     if (strcmp(device, "vnc") == 0) {
         if (read_only) {
             monitor_printf(mon,
@@ -1738,7 +1748,9 @@ void hmp_change(Monitor *mon, const QDict *qdict)
             }
         }
         qmp_change("vnc", target, !!arg, arg, &err);
-    } else {
+    } else
+#endif
+    {
         if (read_only) {
             read_only_mode =
                 qapi_enum_parse(BlockdevChangeReadOnlyMode_lookup,
qmp.c +4 -26
index b86201e349..2c90dacb56 100644
@@ -130,22 +130,6 @@ void qmp_cpu_add(int64_t id, Error **errp)
     }
 }
 
-#ifndef CONFIG_VNC
-/* If VNC support is enabled, the "true" query-vnc command is
-   defined in the VNC subsystem */
-VncInfo *qmp_query_vnc(Error **errp)
-{
-    error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
-    return NULL;
-};
-
-VncInfo2List *qmp_query_vnc_servers(Error **errp)
-{
-    error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
-    return NULL;
-};
-#endif
-
 #ifndef CONFIG_SPICE
 /*
  * qmp-commands.hx ensures that QMP command query-spice exists only
@@ -403,23 +387,17 @@ static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
         qmp_change_vnc_listen(target, errp);
     }
 }
-#else
-void qmp_change_vnc_password(const char *password, Error **errp)
-{
-    error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
-}
-static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
-                           Error **errp)
-{
-    error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
-}
 #endif /* !CONFIG_VNC */
 
 void qmp_change(const char *device, const char *target,
                 bool has_arg, const char *arg, Error **errp)
 {
     if (strcmp(device, "vnc") == 0) {
+#ifdef CONFIG_VNC
         qmp_change_vnc(target, has_arg, arg, errp);
+#else
+        error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
+#endif
     } else {
         qmp_blockdev_change_medium(true, device, false, NULL, target,
                                    has_arg, arg, false, 0, errp);
Signature · 1 line
2.14.0.rc0.1.g40ca67566

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#35 lore raw

Re: [Qemu-devel] [PATCH 16/26] qapi: add conditions to VNC type/commands/events on the schema

Quoted 21 lines · Dr. David Alan Gilbert
Quoted 20 lines · * Marc-André Lureau ([email protected]) wrote:

Add #if defined(CONFIG_VNC) in generated code, and adjust the qmp/hmp code accordingly.

hmp.c +1 -0
index fd80dce758..9454c634bd 100644
@@ -605,6 +605,7 @@ void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
     qapi_free_BlockStatsList(stats_list);
 }
 
+#ifdef CONFIG_VNC
 /* Helper for hmp_info_vnc_clients, _servers */
 static void hmp_info_VncBasicInfo(Monitor *mon, VncBasicInfo *info,
                                   const char *name)
@@ -692,6 +693,12 @@ void hmp_info_vnc(Monitor *mon, const QDict *qdict)
     qapi_free_VncInfo2List(info2l);
 
Quoted 5 lines

hmp.c void hmp_info_vnc(Monitor *mon, const QDict *qdict) · line 699

Quoted 5 lines
hmp.c +4 -0
 }
+#else
+void hmp_info_vnc(Monitor *mon, const QDict *qdict)
+{
+    warn_report("VNC support is disabled");

error_report(), please (see below).

Quoted 10 lines

hmp.c void hmp_info_vnc(Monitor *mon, const QDict *qdict) · line 701

Quoted 2 lines
hmp.c +2 -0
+}
+#endif

I'm OK with this, so

although you might just be able to add a #ifdef in hmp-commands-info.hx and avoid the is disabled function, or you might find that with the QMP returning an error the HMP just passes that error on.

Let's compare failures when !CONFIG_VNC:

(a) Marc-André's patch as is:

(qemu) info vnc
warning: VNC support is disabled

Drop the "warning: " (because it ain't; the command failed), and this is fine.

(b) Compiling them out completely (#ifdef in hmp-commands*.hx):

unknown command: 'vnc'

HMP bug; should be something like

Unknown command: 'info vnc'

but that's not this series' problem.

Good enough for me.

(c) Forwarding the QMP error verbatim

The command query-vnc has not been found

No good.

(d) Handling CommandNotFound

More work than (a) for the same result.

As far as I'm concerned, feel free to do (a) or (b).

[...]

#36 lore raw

Re: [Qemu-devel] [PATCH 16/26] qapi: add conditions to VNC type/commands/events on the schema

Quoted 54 lines · * Markus Armbruster ([email protected]) wrote:
Quoted 21 lines · Dr. David Alan Gilbert
Quoted 20 lines · * Marc-André Lureau ([email protected]) wrote:

Add #if defined(CONFIG_VNC) in generated code, and adjust the qmp/hmp code accordingly.

hmp.c +1 -0
index fd80dce758..9454c634bd 100644
@@ -605,6 +605,7 @@ void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
     qapi_free_BlockStatsList(stats_list);
 }
 
+#ifdef CONFIG_VNC
 /* Helper for hmp_info_vnc_clients, _servers */
 static void hmp_info_VncBasicInfo(Monitor *mon, VncBasicInfo *info,
                                   const char *name)
@@ -692,6 +693,12 @@ void hmp_info_vnc(Monitor *mon, const QDict *qdict)
     qapi_free_VncInfo2List(info2l);
 
Quoted 5 lines

hmp.c void hmp_info_vnc(Monitor *mon, const QDict *qdict) · line 699

Quoted 5 lines
hmp.c +4 -0
 }
+#else
+void hmp_info_vnc(Monitor *mon, const QDict *qdict)
+{
+    warn_report("VNC support is disabled");

error_report(), please (see below).

Quoted 10 lines

hmp.c void hmp_info_vnc(Monitor *mon, const QDict *qdict) · line 701

Quoted 2 lines
hmp.c +2 -0
+}
+#endif

I'm OK with this, so

although you might just be able to add a #ifdef in hmp-commands-info.hx and avoid the is disabled function, or you might find that with the QMP returning an error the HMP just passes that error on.

Let's compare failures when !CONFIG_VNC:

(a) Marc-André's patch as is:

(qemu) info vnc
warning: VNC support is disabled

Drop the "warning: " (because it ain't; the command failed), and this is fine.

(b) Compiling them out completely (#ifdef in hmp-commands*.hx):

Quoted 7 lines

unknown command: 'vnc'

HMP bug; should be something like

Unknown command: 'info vnc'

but that's not this series' problem.

I'll fix that missing 'info'

Dave

Quoted 8 lines

Good enough for me.

(c) Forwarding the QMP error verbatim

The command query-vnc has not been found

No good.

Quoted 7 lines

(d) Handling CommandNotFound

More work than (a) for the same result.

As far as I'm concerned, feel free to do (a) or (b).

[...]

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#37 lore raw

Re: [Qemu-devel] [PATCH 16/26] qapi: add conditions to VNC type/commands/events on the schema

In reply to Marc-André Lureau

Copying our resident VNC maintainer^Wodd fixer Gerd.

Also copying Dan for QCryptoCipherAlgorithm.

Gerd, Dan, this patch is about making VNC support visible in query-qmp-schema, by having the QAPI generators generate suitable ifdeffery. Bonus: no need for QMP command stubs for !defined(CONFIG_VNC).

Quoted 209 lines · Marc-André Lureau

Add #if defined(CONFIG_VNC) in generated code, and adjust the qmp/hmp code accordingly.

---
 qapi-schema.json | 34 ++++++++++++++++++++++------------
 qapi/event.json  |  9 ++++++---
 hmp.c            | 14 +++++++++++++-
 qmp.c            | 30 ++++--------------------------
 4 files changed, 45 insertions(+), 42 deletions(-)
qapi-schema.json +22 -12
index 9c6c3e1a53..829c66f9eb 100644
@@ -1660,7 +1660,8 @@
   'data': { 'host': 'str',
             'service': 'str',
             'family': 'NetworkAddressFamily',
-            'websocket': 'bool' } }
+            'websocket': 'bool' },
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @VncServerInfo:
@@ -1674,7 +1675,8 @@
 ##
 { 'struct': 'VncServerInfo',
   'base': 'VncBasicInfo',
-  'data': { '*auth': 'str' } }
+  'data': { '*auth': 'str' },
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @VncClientInfo:
@@ -1691,7 +1693,8 @@
 ##
 { 'struct': 'VncClientInfo',
   'base': 'VncBasicInfo',
-  'data': { '*x509_dname': 'str', '*sasl_username': 'str' } }
+  'data': { '*x509_dname': 'str', '*sasl_username': 'str' },
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @VncInfo:
@@ -1732,7 +1735,8 @@
 { 'struct': 'VncInfo',
   'data': {'enabled': 'bool', '*host': 'str',
            '*family': 'NetworkAddressFamily',
-           '*service': 'str', '*auth': 'str', '*clients': ['VncClientInfo']} }
+           '*service': 'str', '*auth': 'str', '*clients': ['VncClientInfo']},
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @VncPrimaryAuth:
@@ -1743,7 +1747,8 @@
 ##
 { 'enum': 'VncPrimaryAuth',
   'data': [ 'none', 'vnc', 'ra2', 'ra2ne', 'tight', 'ultra',
-            'tls', 'vencrypt', 'sasl' ] }
+            'tls', 'vencrypt', 'sasl' ],
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @VncVencryptSubAuth:
@@ -1757,7 +1762,8 @@
             'tls-none',  'x509-none',
             'tls-vnc',   'x509-vnc',
             'tls-plain', 'x509-plain',
-            'tls-sasl',  'x509-sasl' ] }
+            'tls-sasl',  'x509-sasl' ],
+  'if': 'defined(CONFIG_VNC)' }
 
 
 ##
@@ -1775,7 +1781,8 @@
 { 'struct': 'VncServerInfo2',
   'base': 'VncBasicInfo',
   'data': { 'auth'      : 'VncPrimaryAuth',
-            '*vencrypt' : 'VncVencryptSubAuth' } }
+            '*vencrypt' : 'VncVencryptSubAuth' },
+  'if': 'defined(CONFIG_VNC)' }
 
 
 ##
@@ -1808,7 +1815,8 @@
             'clients'   : ['VncClientInfo'],
             'auth'      : 'VncPrimaryAuth',
             '*vencrypt' : 'VncVencryptSubAuth',
-            '*display'  : 'str' } }
+            '*display'  : 'str' },
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @query-vnc:
@@ -1839,7 +1847,8 @@
 #    }
 #
 ##
-{ 'command': 'query-vnc', 'returns': 'VncInfo' }
+{ 'command': 'query-vnc', 'returns': 'VncInfo',
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @query-vnc-servers:
@@ -1850,7 +1859,8 @@
 #
 # Since: 2.3
 ##
-{ 'command': 'query-vnc-servers', 'returns': ['VncInfo2'] }
+{ 'command': 'query-vnc-servers', 'returns': ['VncInfo2'],
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @SpiceBasicInfo:
@@ -3077,8 +3087,8 @@
 # Notes:  An empty password in this command will set the password to the empty
 #         string.  Existing clients are unaffected by executing this command.
 ##
-{ 'command': 'change-vnc-password', 'data': {'password': 'str'} }
-
+{ 'command': 'change-vnc-password', 'data': {'password': 'str'},
+  'if': 'defined(CONFIG_VNC)' }
 ##
 # @change:
 #
qapi/event.json +6 -3
index 6d22b025cc..c8b8e9f384 100644
@@ -263,7 +263,8 @@
 ##
 { 'event': 'VNC_CONNECTED',
   'data': { 'server': 'VncServerInfo',
-            'client': 'VncBasicInfo' } }
+            'client': 'VncBasicInfo' },
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @VNC_INITIALIZED:
@@ -290,7 +291,8 @@
 ##
 { 'event': 'VNC_INITIALIZED',
   'data': { 'server': 'VncServerInfo',
-            'client': 'VncClientInfo' } }
+            'client': 'VncClientInfo' },
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @VNC_DISCONNECTED:
@@ -316,7 +318,8 @@
 ##
 { 'event': 'VNC_DISCONNECTED',
   'data': { 'server': 'VncServerInfo',
-            'client': 'VncClientInfo' } }
+            'client': 'VncClientInfo' },
+  'if': 'defined(CONFIG_VNC)' }
 
 ##
 # @SPICE_CONNECTED:
hmp.c +10 -0
index fd80dce758..9454c634bd 100644
@@ -605,6 +605,7 @@ void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
     qapi_free_BlockStatsList(stats_list);
 }
 
+#ifdef CONFIG_VNC
 /* Helper for hmp_info_vnc_clients, _servers */
 static void hmp_info_VncBasicInfo(Monitor *mon, VncBasicInfo *info,
                                   const char *name)
@@ -692,6 +693,12 @@ void hmp_info_vnc(Monitor *mon, const QDict *qdict)
     qapi_free_VncInfo2List(info2l);
 
 }
+#else
+void hmp_info_vnc(Monitor *mon, const QDict *qdict)
+{
+    warn_report("VNC support is disabled");
+}
+#endif
 
 #ifdef CONFIG_SPICE
 void hmp_info_spice(Monitor *mon, const QDict *qdict)
@@ -1708,12 +1715,14 @@ void hmp_eject(Monitor *mon, const QDict *qdict)
     hmp_handle_error(mon, &err);
 }
 
+#ifdef CONFIG_VNC
 static void hmp_change_read_arg(void *opaque, const char *password,
                                 void *readline_opaque)
 {
     qmp_change_vnc_password(password, NULL);
     monitor_read_command(opaque, 1);
 }
+#endif
 
 void hmp_change(Monitor *mon, const QDict *qdict)
 {
@@ -1724,6 +1733,7 @@ void hmp_change(Monitor *mon, const QDict *qdict)
     BlockdevChangeReadOnlyMode read_only_mode = 0;
     Error *err = NULL;
 
+#ifdef CONFIG_VNC
     if (strcmp(device, "vnc") == 0) {
         if (read_only) {
             monitor_printf(mon,

hmp.c void hmp_change(Monitor *mon, const QDict *qdict) · line 1756

Quoted 10 lines
hmp.c +3 -1
@@ -1738,7 +1748,9 @@ void hmp_change(Monitor *mon, const QDict *qdict)
             }
         }
         qmp_change("vnc", target, !!arg, arg, &err);
-    } else {
+    } else
+#endif
+    {
         if (read_only) {
             read_only_mode =
                 qapi_enum_parse(BlockdevChangeReadOnlyMode_lookup,

On HMP, see my reply to Dave's review.

Quoted 45 lines
qmp.c +0 -26
index b86201e349..2c90dacb56 100644
@@ -130,22 +130,6 @@ void qmp_cpu_add(int64_t id, Error **errp)
     }
 }
 
-#ifndef CONFIG_VNC
-/* If VNC support is enabled, the "true" query-vnc command is
-   defined in the VNC subsystem */
-VncInfo *qmp_query_vnc(Error **errp)
-{
-    error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
-    return NULL;
-};
-
-VncInfo2List *qmp_query_vnc_servers(Error **errp)
-{
-    error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
-    return NULL;
-};
-#endif
-
 #ifndef CONFIG_SPICE
 /*
  * qmp-commands.hx ensures that QMP command query-spice exists only
@@ -403,23 +387,17 @@ static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
         qmp_change_vnc_listen(target, errp);
     }
 }
-#else
-void qmp_change_vnc_password(const char *password, Error **errp)
-{
-    error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
-}
-static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
-                           Error **errp)
-{
-    error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
-}
 #endif /* !CONFIG_VNC */
 
 void qmp_change(const char *device, const char *target,
                 bool has_arg, const char *arg, Error **errp)

qmp.c static void qmp_change_vnc(const char *target, bool has_arg, const char *arg, · line 403

Quoted 10 lines
qmp.c +4 -0
 {
     if (strcmp(device, "vnc") == 0) {
+#ifdef CONFIG_VNC
         qmp_change_vnc(target, has_arg, arg, errp);
+#else
+        error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
+#endif
     } else {
         qmp_blockdev_change_medium(true, device, false, NULL, target,
                                    has_arg, arg, false, 0, errp);

Commands you make conditional:

* query-vnc, query-vnc-servers, change-vnc-password

Before the patch, the commands for !CONFIG_VNC are stubs that fail like this:

    {"error": {"class": "GenericError",
               "desc": "The feature 'vnc' is not enabled"}}

Afterwards, they fail like this:

    {"error": {"class": "CommandNotFound",
               "desc": "The command FOO has not been found"}}

I call that an improvement, because it lets clients distinguish between command unavailable (class CommandNotFound) and command failed (class GenericError).

Events you make conditional:

* VNC_CONNECTED, VNC_INITIALIZED, VNC_DISCONNECTED

Now let me check for completeness. Occurrences of VNC (case insensitive) in the schema that aren't covered by your changes:

* add_client

Command has other uses, including "socket bases character devices". These are unconditional as far as I can tell. Good.

* set_password, expire_password

In theory, these commands could be used for managing any service's password. In practice, they're used for VNC and SPICE services. They're documented for "remote display session" / "remote display server".

The service is selected by argument @protocol. The code special-cases protocol-specific argument checking, then calls a protocol-specific function to do the work. If it fails, the command fails with "Could not set password". It does when the service isn't compiled in (it's a stub then).

We could make these commands conditional on the conjunction of all services [currently: defined(CONFIG_VNC) || defined(CONFIG_SPICE)], but I doubt it's worthwhile.

Okay.

* change

Command has other uses, namely changing media.

Your patch inlines a stub; no functional change. Good.

* QCryptoCipherAlgorithm

This:

# @des-rfb: RFB specific variant of single DES. Do not use except in VNC.

I guess we could compile this out if we wanted to. I doubt we do, but Dan might have other ideas.

Some of this analysis should perhaps be worked into the commit message.

Overall, the schema syntax works nicely for me. A bit on the verbose side perhaps, but I like that the conditions are locally obvious.

Observation: we got >250 lines of VNC stuff in qapi-schema.json. Moving them into qapi/vnc.json would permit proper MAINTAINERS coverage. Gerd, what do you think?

#38 lore raw

Re: [Qemu-devel] [PATCH 16/26] qapi: add conditions to VNC type/commands/events on the schema

Gerd, can we delete the vnc_init_func() stub? Things still compile for me when I do.

include/ui/console.h +0 -5
index 7262bef..2c3b2cd 100644
@@ -484,11 +484,6 @@ static inline QemuOpts *vnc_parse(const char *str, Error **errp)
     error_setg(errp, "VNC support is disabled");
     return NULL;
 }
-static inline int vnc_init_func(void *opaque, QemuOpts *opts, Error **errp)
-{
-    error_setg(errp, "VNC support is disabled");
-    return -1;
-}
 #endif
 
 /* curses.c */
#39 lore raw

Re: [Qemu-devel] [PATCH 16/26] qapi: add conditions to VNC type/commands/events on the schema

Quoted 10 lines · Markus Armbruster

Gerd, can we delete the vnc_init_func() stub?  Things still compile for
me when I do.

include/ui/console.h +0 -0
index 7262bef..2c3b2cd 100644
@@ -484,11 +484,6 @@ static inline QemuOpts *vnc_parse(const char
 *str, Error **errp)

include/ui/console.h static inline QemuOpts *vnc_parse(const char · line 489

Quoted 10 lines
include/ui/console.h +0 -5
      error_setg(errp, "VNC support is disabled");
      return NULL;
  }
-static inline int vnc_init_func(void *opaque, QemuOpts *opts, Error
 **errp)
-{
-    error_setg(errp, "VNC support is disabled");
-    return -1;
-}
  #endif

Looking at a663fbd9e2f65fae81018d81f231ad79510cf9fb. Guess we want fix qemu_opts_foreach to not barf on NULL, then revert that commit, so f8c75b2486 (adding Eduardo to Cc) works as intended.

Beside that: The inline for vnc_init_func looks pointless (it's used via function pointer). Also it can be empty as it will never be actually called. Which is fine, vnc_parse will throw an error if someone tries to use vnc with a binary without vnc support.

cheers,
Gerd

#40 lore raw

Re: [Qemu-devel] [PATCH 16/26] qapi: add conditions to VNC type/commands/events on the schema

Quoted 17 lines · Gerd Hoffmann
Quoted 16 lines · Markus Armbruster

Gerd, can we delete the vnc_init_func() stub?  Things still compile for
me when I do.

include/ui/console.h +0 -2
index 7262bef..2c3b2cd 100644
@@ -484,11 +484,6 @@ static inline QemuOpts *vnc_parse(const char
 *str, Error **errp)
      error_setg(errp, "VNC support is disabled");
      return NULL;
  }
-static inline int vnc_init_func(void *opaque, QemuOpts *opts, Error
 **errp)
-{
Quoted 8 lines

include/ui/console.h static inline QemuOpts *vnc_parse(const char *str, Error **errp) · line 487

Quoted 4 lines
include/ui/console.h +0 -3
-    error_setg(errp, "VNC support is disabled");
-    return -1;
-}
  #endif

Looking at a663fbd9e2f65fae81018d81f231ad79510cf9fb. Guess we want fix qemu_opts_foreach to not barf on NULL, then revert that commit, so f8c75b2486 (adding Eduardo to Cc) works as intended.

I agree with that plan, but I wouldn't mind having the stub removed in the meantime.

Quoted 5 lines

Beside that: The inline for vnc_init_func looks pointless (it's used via function pointer). Also it can be empty as it will never be actually called. Which is fine, vnc_parse will throw an error if someone tries to use vnc with a binary without vnc support.

True.

Signature · 1 line
Eduardo
#41 lore raw

Re: [Qemu-devel] [PATCH 16/26] qapi: add conditions to VNC type/commands/events on the schema

In reply to Markus Armbruster

Hi,

Quoted 5 lines

Observation: we got >250 lines of VNC stuff in qapi- schema.json.  Moving
them into qapi/vnc.json would permit proper MAINTAINERS coverage.  Gerd,
what do you think?

Yes, looks useful to me.

cheers,
Gerd

#42 lore raw

Re: [Qemu-devel] [PATCH 16/26] qapi: add conditions to VNC type/commands/events on the schema

In reply to Markus Armbruster
Quoted 8 lines · Markus Armbruster

Copying our resident VNC maintainer^Wodd fixer Gerd.

Also copying Dan for QCryptoCipherAlgorithm.

Gerd, Dan, this patch is about making VNC support visible in query-qmp-schema, by having the QAPI generators generate suitable ifdeffery. Bonus: no need for QMP command stubs for !defined(CONFIG_VNC).

[snip]

Quoted 8 lines

* QCryptoCipherAlgorithm

This:

# @des-rfb: RFB specific variant of single DES. Do not use except in VNC.

I guess we could compile this out if we wanted to. I doubt we do, but Dan might have other ideas.

It isn't worth the effort to make that conditionally compiled. We getting DES from nettle/gcrypt these days, instead of our custom in-tree desrfb.c file, so its no burden to have it.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#43 lore raw

Re: [Qemu-devel] [PATCH 16/26] qapi: add conditions to VNC type/commands/events on the schema

Hi

----- Original Message -----

Quoted 19 lines · Markus Armbruster
Quoted 8 lines

Copying our resident VNC maintainer^Wodd fixer Gerd.

Also copying Dan for QCryptoCipherAlgorithm.

Gerd, Dan, this patch is about making VNC support visible in query-qmp-schema, by having the QAPI generators generate suitable ifdeffery. Bonus: no need for QMP command stubs for !defined(CONFIG_VNC).

[snip]

Quoted 7 lines

* QCryptoCipherAlgorithm

This:

# @des-rfb: RFB specific variant of single DES. Do not use except in VNC.

Quoted 6 lines
Quoted 2 lines

I guess we could compile this out if we wanted to. I doubt we do, but Dan might have other ideas.

It isn't worth the effort to make that conditionally compiled. We getting DES from nettle/gcrypt these days, instead of our custom in-tree desrfb.c file, so its no burden to have it.

fyi, the last iteration of the patch made it conditional too.

#44 lore raw

PATCH 17/26 qapi: add conditions to SPICE type/commands/events on the schema

In reply to Marc-André Lureau

Add #if defined(CONFIG_SPICE) in generated code, and adjust the qmp/hmp code accordingly.

---
 qapi-schema.json | 28 ++++++++++++++++++----------
 qapi/event.json  | 12 ++++++++----
 monitor.c        |  3 ---
 qmp.c            | 16 ----------------
 4 files changed, 26 insertions(+), 33 deletions(-)
qapi-schema.json +18 -10
index 829c66f9eb..bcee3157b0 100644
@@ -1878,7 +1878,8 @@
 { 'struct': 'SpiceBasicInfo',
   'data': { 'host': 'str',
             'port': 'str',
-            'family': 'NetworkAddressFamily' } }
+            'family': 'NetworkAddressFamily' },
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @SpiceServerInfo:
@@ -1891,7 +1892,8 @@
 ##
 { 'struct': 'SpiceServerInfo',
   'base': 'SpiceBasicInfo',
-  'data': { '*auth': 'str' } }
+  'data': { '*auth': 'str' },
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @SpiceChannel:
@@ -1916,7 +1918,8 @@
 { 'struct': 'SpiceChannel',
   'base': 'SpiceBasicInfo',
   'data': {'connection-id': 'int', 'channel-type': 'int', 'channel-id': 'int',
-           'tls': 'bool'} }
+           'tls': 'bool'},
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @SpiceQueryMouseMode:
@@ -1935,7 +1938,8 @@
 # Since: 1.1
 ##
 { 'enum': 'SpiceQueryMouseMode',
-  'data': [ 'client', 'server', 'unknown' ] }
+  'data': [ 'client', 'server', 'unknown' ],
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @SpiceInfo:
@@ -1972,7 +1976,8 @@
 { 'struct': 'SpiceInfo',
   'data': {'enabled': 'bool', 'migrated': 'bool', '*host': 'str', '*port': 'int',
            '*tls-port': 'int', '*auth': 'str', '*compiled-version': 'str',
-           'mouse-mode': 'SpiceQueryMouseMode', '*channels': ['SpiceChannel']} }
+           'mouse-mode': 'SpiceQueryMouseMode', '*channels': ['SpiceChannel']},
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @query-spice:
@@ -2017,7 +2022,8 @@
 #    }
 #
 ##
-{ 'command': 'query-spice', 'returns': 'SpiceInfo' }
+{ 'command': 'query-spice', 'returns': 'SpiceInfo',
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @BalloonInfo:
@@ -5067,7 +5073,8 @@
 # Since: 1.5
 ##
 { 'struct': 'ChardevSpiceChannel', 'data': { 'type'  : 'str' },
-  'base': 'ChardevCommon' }
+  'base': 'ChardevCommon',
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @ChardevSpicePort:
@@ -5079,7 +5086,8 @@
 # Since: 1.5
 ##
 { 'struct': 'ChardevSpicePort', 'data': { 'fqdn'  : 'str' },
-  'base': 'ChardevCommon' }
+  'base': 'ChardevCommon',
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @ChardevVC:
@@ -5133,8 +5141,8 @@
                                        'testdev': 'ChardevCommon',
                                        'stdio'  : 'ChardevStdio',
                                        'console': 'ChardevCommon',
-                                       'spicevmc' : 'ChardevSpiceChannel',
-                                       'spiceport' : 'ChardevSpicePort',
+                                       'spicevmc' : { 'type': 'ChardevSpiceChannel', 'if': 'defined(CONFIG_SPICE)' },
+                                       'spiceport' : { 'type': 'ChardevSpicePort', 'if': 'defined(CONFIG_SPICE)' },
                                        'vc'     : 'ChardevVC',
                                        'ringbuf': 'ChardevRingbuf',
                                        # next one is just for compatibility
qapi/event.json +8 -4
index c8b8e9f384..ff59551914 100644
@@ -344,7 +344,8 @@
 ##
 { 'event': 'SPICE_CONNECTED',
   'data': { 'server': 'SpiceBasicInfo',
-            'client': 'SpiceBasicInfo' } }
+            'client': 'SpiceBasicInfo' },
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @SPICE_INITIALIZED:
@@ -372,7 +373,8 @@
 ##
 { 'event': 'SPICE_INITIALIZED',
   'data': { 'server': 'SpiceServerInfo',
-            'client': 'SpiceChannel' } }
+            'client': 'SpiceChannel' },
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @SPICE_DISCONNECTED:
@@ -397,7 +399,8 @@
 ##
 { 'event': 'SPICE_DISCONNECTED',
   'data': { 'server': 'SpiceBasicInfo',
-            'client': 'SpiceBasicInfo' } }
+            'client': 'SpiceBasicInfo' },
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @SPICE_MIGRATE_COMPLETED:
@@ -412,7 +415,8 @@
 #      "event": "SPICE_MIGRATE_COMPLETED" }
 #
 ##
-{ 'event': 'SPICE_MIGRATE_COMPLETED' }
+{ 'event': 'SPICE_MIGRATE_COMPLETED',
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @MIGRATION:
monitor.c +0 -3
index a1773d5bc7..4bf6a3ea2e 100644
@@ -970,9 +970,6 @@ static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
  */
 static void qmp_unregister_commands_hack(void)
 {
-#ifndef CONFIG_SPICE
-    qmp_unregister_command(&qmp_commands, "query-spice");
-#endif
 #ifndef CONFIG_REPLICATION
     qmp_unregister_command(&qmp_commands, "xen-set-replication");
     qmp_unregister_command(&qmp_commands, "query-xen-replication-status");
qmp.c +0 -16
index 2c90dacb56..90816ba283 100644
@@ -130,22 +130,6 @@ void qmp_cpu_add(int64_t id, Error **errp)
     }
 }
 
-#ifndef CONFIG_SPICE
-/*
- * qmp-commands.hx ensures that QMP command query-spice exists only
- * #ifdef CONFIG_SPICE.  Necessary for an accurate query-commands
- * result.  However, the QAPI schema is blissfully unaware of that,
- * and the QAPI code generator happily generates a dead
- * qmp_marshal_query_spice() that calls qmp_query_spice().  Provide it
- * one, or else linking fails.  FIXME Educate the QAPI schema on
- * CONFIG_SPICE.
- */
-SpiceInfo *qmp_query_spice(Error **errp)
-{
-    abort();
-};
-#endif
-
 void qmp_cont(Error **errp)
 {
     BlockBackend *blk;
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#45 lore raw

Re: [Qemu-devel] [PATCH 17/26] qapi: add conditions to SPICE type/commands/events on the schema

Copying our resident SPICE maintainer Gerd.

Quoted 181 lines · Marc-André Lureau

Add #if defined(CONFIG_SPICE) in generated code, and adjust the qmp/hmp code accordingly.

---
 qapi-schema.json | 28 ++++++++++++++++++----------
 qapi/event.json  | 12 ++++++++----
 monitor.c        |  3 ---
 qmp.c            | 16 ----------------
 4 files changed, 26 insertions(+), 33 deletions(-)
qapi-schema.json +18 -10
index 829c66f9eb..bcee3157b0 100644
@@ -1878,7 +1878,8 @@
 { 'struct': 'SpiceBasicInfo',
   'data': { 'host': 'str',
             'port': 'str',
-            'family': 'NetworkAddressFamily' } }
+            'family': 'NetworkAddressFamily' },
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @SpiceServerInfo:
@@ -1891,7 +1892,8 @@
 ##
 { 'struct': 'SpiceServerInfo',
   'base': 'SpiceBasicInfo',
-  'data': { '*auth': 'str' } }
+  'data': { '*auth': 'str' },
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @SpiceChannel:
@@ -1916,7 +1918,8 @@
 { 'struct': 'SpiceChannel',
   'base': 'SpiceBasicInfo',
   'data': {'connection-id': 'int', 'channel-type': 'int', 'channel-id': 'int',
-           'tls': 'bool'} }
+           'tls': 'bool'},
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @SpiceQueryMouseMode:
@@ -1935,7 +1938,8 @@
 # Since: 1.1
 ##
 { 'enum': 'SpiceQueryMouseMode',
-  'data': [ 'client', 'server', 'unknown' ] }
+  'data': [ 'client', 'server', 'unknown' ],
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @SpiceInfo:
@@ -1972,7 +1976,8 @@
 { 'struct': 'SpiceInfo',
   'data': {'enabled': 'bool', 'migrated': 'bool', '*host': 'str', '*port': 'int',
            '*tls-port': 'int', '*auth': 'str', '*compiled-version': 'str',
-           'mouse-mode': 'SpiceQueryMouseMode', '*channels': ['SpiceChannel']} }
+           'mouse-mode': 'SpiceQueryMouseMode', '*channels': ['SpiceChannel']},
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @query-spice:
@@ -2017,7 +2022,8 @@
 #    }
 #
 ##
-{ 'command': 'query-spice', 'returns': 'SpiceInfo' }
+{ 'command': 'query-spice', 'returns': 'SpiceInfo',
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @BalloonInfo:
@@ -5067,7 +5073,8 @@
 # Since: 1.5
 ##
 { 'struct': 'ChardevSpiceChannel', 'data': { 'type'  : 'str' },
-  'base': 'ChardevCommon' }
+  'base': 'ChardevCommon',
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @ChardevSpicePort:
@@ -5079,7 +5086,8 @@
 # Since: 1.5
 ##
 { 'struct': 'ChardevSpicePort', 'data': { 'fqdn'  : 'str' },
-  'base': 'ChardevCommon' }
+  'base': 'ChardevCommon',
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @ChardevVC:
@@ -5133,8 +5141,8 @@
                                        'testdev': 'ChardevCommon',
                                        'stdio'  : 'ChardevStdio',
                                        'console': 'ChardevCommon',
-                                       'spicevmc' : 'ChardevSpiceChannel',
-                                       'spiceport' : 'ChardevSpicePort',
+                                       'spicevmc' : { 'type': 'ChardevSpiceChannel', 'if': 'defined(CONFIG_SPICE)' },
+                                       'spiceport' : { 'type': 'ChardevSpicePort', 'if': 'defined(CONFIG_SPICE)' },
                                        'vc'     : 'ChardevVC',
                                        'ringbuf': 'ChardevRingbuf',
                                        # next one is just for compatibility
qapi/event.json +8 -4
index c8b8e9f384..ff59551914 100644
@@ -344,7 +344,8 @@
 ##
 { 'event': 'SPICE_CONNECTED',
   'data': { 'server': 'SpiceBasicInfo',
-            'client': 'SpiceBasicInfo' } }
+            'client': 'SpiceBasicInfo' },
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @SPICE_INITIALIZED:
@@ -372,7 +373,8 @@
 ##
 { 'event': 'SPICE_INITIALIZED',
   'data': { 'server': 'SpiceServerInfo',
-            'client': 'SpiceChannel' } }
+            'client': 'SpiceChannel' },
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @SPICE_DISCONNECTED:
@@ -397,7 +399,8 @@
 ##
 { 'event': 'SPICE_DISCONNECTED',
   'data': { 'server': 'SpiceBasicInfo',
-            'client': 'SpiceBasicInfo' } }
+            'client': 'SpiceBasicInfo' },
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @SPICE_MIGRATE_COMPLETED:
@@ -412,7 +415,8 @@
 #      "event": "SPICE_MIGRATE_COMPLETED" }
 #
 ##
-{ 'event': 'SPICE_MIGRATE_COMPLETED' }
+{ 'event': 'SPICE_MIGRATE_COMPLETED',
+  'if': 'defined(CONFIG_SPICE)' }
 
 ##
 # @MIGRATION:
monitor.c +0 -3
index a1773d5bc7..4bf6a3ea2e 100644
@@ -970,9 +970,6 @@ static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
  */
 static void qmp_unregister_commands_hack(void)
 {
-#ifndef CONFIG_SPICE
-    qmp_unregister_command(&qmp_commands, "query-spice");
-#endif
 #ifndef CONFIG_REPLICATION
     qmp_unregister_command(&qmp_commands, "xen-set-replication");
     qmp_unregister_command(&qmp_commands, "query-xen-replication-status");
qmp.c +0 -9
index 2c90dacb56..90816ba283 100644
@@ -130,22 +130,6 @@ void qmp_cpu_add(int64_t id, Error **errp)
     }
 }
 
-#ifndef CONFIG_SPICE
-/*
- * qmp-commands.hx ensures that QMP command query-spice exists only
- * #ifdef CONFIG_SPICE.  Necessary for an accurate query-commands
- * result.  However, the QAPI schema is blissfully unaware of that,
- * and the QAPI code generator happily generates a dead
- * qmp_marshal_query_spice() that calls qmp_query_spice().  Provide it
- * one, or else linking fails.  FIXME Educate the QAPI schema on
- * CONFIG_SPICE.

qmp.c void qmp_cpu_add(int64_t id, Error **errp) · line 135

Quoted 10 lines
qmp.c +0 -7
- */
-SpiceInfo *qmp_query_spice(Error **errp)
-{
-    abort();
-};
-#endif
-
 void qmp_cont(Error **errp)
 {
     BlockBackend *blk;

Same drill as for PATCH 16.

Commands you make conditional:

* query-spice

Before the patch, we first register the unconditionally command in generated code (requires a stub), then conditionally unregister in qmp_unregister_commands_hack().

Afterwards, we register only when CONFIG_SPICE. The command fails exactly the same, with CommandNotFound.

Improvement, because now query-qmp-schema is accurate, and we're one step close to killing qmp_unregister_commands_hack().

Command arguments you make conditional:

* chardev-add, chardev-change variants spicevmc, spiceport

Before the patch, "spicevmc" and "spiceport" are valid values of "type", but char_get_class() can't map them to a QOM type name, and fails with "'FOO' is not a valid char driver name".

Afterwards, they aren't valid values, and input_type_enum() rejects them with "Invalid parameter 'FOO'". That error message could use improvement, but that's not this patch's problem.

Events you make conditional:

* SPICE_CONNECTED, SPICE_INITIALIZED, SPICE_DISCONNECTED, SPICE_MIGRATE_COMPLETED

Check for completeness by reviewing the case insensitive occurrences of SPICE in the schema not covered by your patch:

* add_client

Same rationale as for VNC (see my review of PATCH 16). Good.

* query-chardev-backends

No conditional, because ChardevBackendInfo member @name is 'str'. I think it should be an enum instead, and then we'd need to make some values conditional. Not this patch's problem.

* client_migrate_info

Similar to set_password and expire_password, this is generic in theory, documented for just "remote display", and implemented just for SPICE, with a stub for !CONFIG_SPICE. Okay for the same reasons set_password and expire_password are okay.

* set_password, expire_password

Same rationale as for VNC (see my review of PATCH 16). Okay.

Some of this analysis should perhaps be worked into the commit message.

#46 lore raw

Re: [Qemu-devel] [PATCH 17/26] qapi: add conditions to SPICE type/commands/events on the schema

Markus Armbruster <[email protected]> writes:

[...]

Quoted 6 lines

Check for completeness by reviewing the case insensitive occurrences of SPICE in the schema not covered by your patch:

* add_client

Same rationale as for VNC (see my review of PATCH 16). Good.

Hmm, there's a difference to VNC, in qmp_add_client():

    if (strcmp(protocol, "spice") == 0) {
        if (!qemu_using_spice(errp)) {
            close(fd);
            return;
        }
        skipauth = has_skipauth ? skipauth : false;
        tls = has_tls ? tls : false;
        if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
            error_setg(errp, "spice failed to add client");
            close(fd);
        }
        return;
#ifdef CONFIG_VNC
    } else if (strcmp(protocol, "vnc") == 0) {
        skipauth = has_skipauth ? skipauth : false;
        vnc_display_add_client(NULL, fd, skipauth);
        return;
#endif
    } else if ((s = qemu_chr_find(protocol)) != NULL) {
        if (qemu_chr_add_client(s, fd) < 0) {
            error_setg(errp, "failed to add client");
            close(fd);
            return;
        }
        return;
    }

    error_setg(errp, "protocol '%s' is invalid", protocol);
    close(fd);

Observe:

* Protocol "spice" is always recognized. Without CONFIG_SPICE, using_spice is always false, and the command fails with qemu_using_spice()'s error "SPICE is not in use".

* Protocol "vnc" is only recognized with CONFIG_VNC. Without, it's rejected with "protocol 'vnc' is invalid".

* If you name your character device "spice" or "vnc", you can't use it with add_client. This is a design flaw.

Except you can use one named "vnc" when !CONFIG_VNC.

I'm afraid the command needs to be replaced. Not in this series, of course.

[...]

#47 lore raw

PATCH 18/26 qapi: add conditions to REPLICATION type/commands on the schema

In reply to Marc-André Lureau

Add #if defined(CONFIG_REPLICATION) in generated code, and adjust the code accordingly.

---
 qapi-schema.json | 12 ++++++++----
 migration/colo.c | 14 ++------------
 monitor.c        |  5 -----
 3 files changed, 10 insertions(+), 21 deletions(-)
qapi-schema.json +8 -4
index bcee3157b0..2f4528c769 100644
@@ -6337,7 +6337,8 @@
 # Since: 2.9
 ##
 { 'command': 'xen-set-replication',
-  'data': { 'enable': 'bool', 'primary': 'bool', '*failover' : 'bool' } }
+  'data': { 'enable': 'bool', 'primary': 'bool', '*failover' : 'bool' },
+  'if': 'defined(CONFIG_REPLICATION)' }
 
 ##
 # @ReplicationStatus:
@@ -6352,7 +6353,8 @@
 # Since: 2.9
 ##
 { 'struct': 'ReplicationStatus',
-  'data': { 'error': 'bool', '*desc': 'str' } }
+  'data': { 'error': 'bool', '*desc': 'str' },
+  'if': 'defined(CONFIG_REPLICATION)' }
 
 ##
 # @query-xen-replication-status:
@@ -6369,7 +6371,8 @@
 # Since: 2.9
 ##
 { 'command': 'query-xen-replication-status',
-  'returns': 'ReplicationStatus' }
+  'returns': 'ReplicationStatus',
+  'if': 'defined(CONFIG_REPLICATION)' }
 
 ##
 # @xen-colo-do-checkpoint:
@@ -6385,7 +6388,8 @@
 #
 # Since: 2.9
 ##
-{ 'command': 'xen-colo-do-checkpoint' }
+{ 'command': 'xen-colo-do-checkpoint',
+  'if': 'defined(CONFIG_REPLICATION)' }
 
 ##
 # @GICCapability:
migration/colo.c +2 -12
index a4255432ac..3bff9fc9a4 100644
@@ -147,11 +147,11 @@ void colo_do_failover(MigrationState *s)
     }
 }
 
+#ifdef CONFIG_REPLICATION
 void qmp_xen_set_replication(bool enable, bool primary,
                              bool has_failover, bool failover,
                              Error **errp)
 {
-#ifdef CONFIG_REPLICATION
     ReplicationMode mode = primary ?
                            REPLICATION_MODE_PRIMARY :
                            REPLICATION_MODE_SECONDARY;
@@ -170,14 +170,10 @@ void qmp_xen_set_replication(bool enable, bool primary,
         }
         replication_stop_all(failover, failover ? NULL : errp);
     }
-#else
-    abort();
-#endif
 }
 
 ReplicationStatus *qmp_query_xen_replication_status(Error **errp)
 {
-#ifdef CONFIG_REPLICATION
     Error *err = NULL;
     ReplicationStatus *s = g_new0(ReplicationStatus, 1);
 
@@ -192,19 +188,13 @@ ReplicationStatus *qmp_query_xen_replication_status(Error **errp)
 
     error_free(err);
     return s;
-#else
-    abort();
-#endif
 }
 
 void qmp_xen_colo_do_checkpoint(Error **errp)
 {
-#ifdef CONFIG_REPLICATION
     replication_do_checkpoint_all(errp);
-#else
-    abort();
-#endif
 }
+#endif
 
 static void colo_send_message(QEMUFile *f, COLOMessage msg,
                               Error **errp)
monitor.c +0 -5
index 4bf6a3ea2e..383c84d162 100644
@@ -970,11 +970,6 @@ static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
  */
 static void qmp_unregister_commands_hack(void)
 {
-#ifndef CONFIG_REPLICATION
-    qmp_unregister_command(&qmp_commands, "xen-set-replication");
-    qmp_unregister_command(&qmp_commands, "query-xen-replication-status");
-    qmp_unregister_command(&qmp_commands, "xen-colo-do-checkpoint");
-#endif
 #ifndef TARGET_I386
     qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
 #endif
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#48 lore raw

Re: [Qemu-devel] [PATCH 18/26] qapi: add conditions to REPLICATION type/commands on the schema

Quoted 113 lines · Marc-André Lureau

Add #if defined(CONFIG_REPLICATION) in generated code, and adjust the code accordingly.

---
 qapi-schema.json | 12 ++++++++----
 migration/colo.c | 14 ++------------
 monitor.c        |  5 -----
 3 files changed, 10 insertions(+), 21 deletions(-)
qapi-schema.json +8 -4
index bcee3157b0..2f4528c769 100644
@@ -6337,7 +6337,8 @@
 # Since: 2.9
 ##
 { 'command': 'xen-set-replication',
-  'data': { 'enable': 'bool', 'primary': 'bool', '*failover' : 'bool' } }
+  'data': { 'enable': 'bool', 'primary': 'bool', '*failover' : 'bool' },
+  'if': 'defined(CONFIG_REPLICATION)' }
 
 ##
 # @ReplicationStatus:
@@ -6352,7 +6353,8 @@
 # Since: 2.9
 ##
 { 'struct': 'ReplicationStatus',
-  'data': { 'error': 'bool', '*desc': 'str' } }
+  'data': { 'error': 'bool', '*desc': 'str' },
+  'if': 'defined(CONFIG_REPLICATION)' }
 
 ##
 # @query-xen-replication-status:
@@ -6369,7 +6371,8 @@
 # Since: 2.9
 ##
 { 'command': 'query-xen-replication-status',
-  'returns': 'ReplicationStatus' }
+  'returns': 'ReplicationStatus',
+  'if': 'defined(CONFIG_REPLICATION)' }
 
 ##
 # @xen-colo-do-checkpoint:
@@ -6385,7 +6388,8 @@
 #
 # Since: 2.9
 ##
-{ 'command': 'xen-colo-do-checkpoint' }
+{ 'command': 'xen-colo-do-checkpoint',
+  'if': 'defined(CONFIG_REPLICATION)' }
 
 ##
 # @GICCapability:
migration/colo.c +2 -12
index a4255432ac..3bff9fc9a4 100644
@@ -147,11 +147,11 @@ void colo_do_failover(MigrationState *s)
     }
 }
 
+#ifdef CONFIG_REPLICATION
 void qmp_xen_set_replication(bool enable, bool primary,
                              bool has_failover, bool failover,
                              Error **errp)
 {
-#ifdef CONFIG_REPLICATION
     ReplicationMode mode = primary ?
                            REPLICATION_MODE_PRIMARY :
                            REPLICATION_MODE_SECONDARY;
@@ -170,14 +170,10 @@ void qmp_xen_set_replication(bool enable, bool primary,
         }
         replication_stop_all(failover, failover ? NULL : errp);
     }
-#else
-    abort();
-#endif
 }
 
 ReplicationStatus *qmp_query_xen_replication_status(Error **errp)
 {
-#ifdef CONFIG_REPLICATION
     Error *err = NULL;
     ReplicationStatus *s = g_new0(ReplicationStatus, 1);
 
@@ -192,19 +188,13 @@ ReplicationStatus *qmp_query_xen_replication_status(Error **errp)
 
     error_free(err);
     return s;
-#else
-    abort();
-#endif
 }
 
 void qmp_xen_colo_do_checkpoint(Error **errp)
 {
-#ifdef CONFIG_REPLICATION
     replication_do_checkpoint_all(errp);
-#else
-    abort();
-#endif
 }
+#endif
 
 static void colo_send_message(QEMUFile *f, COLOMessage msg,
                               Error **errp)
monitor.c +0 -0
index 4bf6a3ea2e..383c84d162 100644
@@ -970,11 +970,6 @@ static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
  */

monitor.c static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data, · line 975

Quoted 10 lines
monitor.c +0 -5
 static void qmp_unregister_commands_hack(void)
 {
-#ifndef CONFIG_REPLICATION
-    qmp_unregister_command(&qmp_commands, "xen-set-replication");
-    qmp_unregister_command(&qmp_commands, "query-xen-replication-status");
-    qmp_unregister_command(&qmp_commands, "xen-colo-do-checkpoint");
-#endif
 #ifndef TARGET_I386
     qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
 #endif

Same drill as for PATCH 16.

Commands you make conditional:

* xen-set-replication, query-xen-replication-status, xen-colo-do-checkpoint

Before the patch, we first register the commands unconditionally in generated code (requires a stub), then conditionally unregister in qmp_unregister_commands_hack().

Afterwards, we register only when CONFIG_REPLICATION. The command fails exactly the same, with CommandNotFound.

Improvement, because now query-qmp-schema is accurate, and we're one step close to killing qmp_unregister_commands_hack().

Check for completeness by reviewing the case insensitive occurrences of replication in the schema not covered by your patch, and review uses of CONFIG_REPLICATION for possible schema connections (I did that for CONFIG_VNC [PATCH 16] and CONFIG_SPICE [PATCH 17], too):

* enum BlockdevDriver value "replication" in command blockdev-add

* Makefile.objs:block-obj-$(CONFIG_REPLICATION) += replication.o block/Makefile.objs:block-obj-$(CONFIG_REPLICATION) += replication.o

I think the following should be ifdef CONFIG_REPLICATION: enum BlockdevDriver value @replication BlockdevOptions variant @replication, BlockdevOptionsReplication, BlockdevOptionsReplicationMode.

Some of this analysis should perhaps be worked into the commit message.

#49 lore raw

Re: [Qemu-devel] [PATCH 18/26] qapi: add conditions to REPLICATION type/commands on the schema

Hi

Quoted 148 lines · Markus Armbruster
Quoted 113 lines · Marc-André Lureau

Add #if defined(CONFIG_REPLICATION) in generated code, and adjust the code accordingly.

---
 qapi-schema.json | 12 ++++++++----
 migration/colo.c | 14 ++------------
 monitor.c        |  5 -----
 3 files changed, 10 insertions(+), 21 deletions(-)
qapi-schema.json +8 -4
index bcee3157b0..2f4528c769 100644
@@ -6337,7 +6337,8 @@
 # Since: 2.9
 ##
 { 'command': 'xen-set-replication',
-  'data': { 'enable': 'bool', 'primary': 'bool', '*failover' : 'bool' } }
+  'data': { 'enable': 'bool', 'primary': 'bool', '*failover' : 'bool' },
+  'if': 'defined(CONFIG_REPLICATION)' }
 
 ##
 # @ReplicationStatus:
@@ -6352,7 +6353,8 @@
 # Since: 2.9
 ##
 { 'struct': 'ReplicationStatus',
-  'data': { 'error': 'bool', '*desc': 'str' } }
+  'data': { 'error': 'bool', '*desc': 'str' },
+  'if': 'defined(CONFIG_REPLICATION)' }
 
 ##
 # @query-xen-replication-status:
@@ -6369,7 +6371,8 @@
 # Since: 2.9
 ##
 { 'command': 'query-xen-replication-status',
-  'returns': 'ReplicationStatus' }
+  'returns': 'ReplicationStatus',
+  'if': 'defined(CONFIG_REPLICATION)' }
 
 ##
 # @xen-colo-do-checkpoint:
@@ -6385,7 +6388,8 @@
 #
 # Since: 2.9
 ##
-{ 'command': 'xen-colo-do-checkpoint' }
+{ 'command': 'xen-colo-do-checkpoint',
+  'if': 'defined(CONFIG_REPLICATION)' }
 
 ##
 # @GICCapability:
migration/colo.c +2 -12
index a4255432ac..3bff9fc9a4 100644
@@ -147,11 +147,11 @@ void colo_do_failover(MigrationState *s)
     }
 }
 
+#ifdef CONFIG_REPLICATION
 void qmp_xen_set_replication(bool enable, bool primary,
                              bool has_failover, bool failover,
                              Error **errp)
 {
-#ifdef CONFIG_REPLICATION
     ReplicationMode mode = primary ?
                            REPLICATION_MODE_PRIMARY :
                            REPLICATION_MODE_SECONDARY;
@@ -170,14 +170,10 @@ void qmp_xen_set_replication(bool enable, bool primary,
         }
         replication_stop_all(failover, failover ? NULL : errp);
     }
-#else
-    abort();
-#endif
 }
 
 ReplicationStatus *qmp_query_xen_replication_status(Error **errp)
 {
-#ifdef CONFIG_REPLICATION
     Error *err = NULL;
     ReplicationStatus *s = g_new0(ReplicationStatus, 1);
 
@@ -192,19 +188,13 @@ ReplicationStatus *qmp_query_xen_replication_status(Error **errp)
 
     error_free(err);
     return s;
-#else
-    abort();
-#endif
 }
 
 void qmp_xen_colo_do_checkpoint(Error **errp)
 {
-#ifdef CONFIG_REPLICATION
     replication_do_checkpoint_all(errp);
-#else
-    abort();
-#endif
 }
+#endif
 
 static void colo_send_message(QEMUFile *f, COLOMessage msg,
                               Error **errp)
monitor.c +0 -0
index 4bf6a3ea2e..383c84d162 100644
@@ -970,11 +970,6 @@ static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
  */

monitor.c static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data, · line 975

Quoted 10 lines
monitor.c +0 -5
 static void qmp_unregister_commands_hack(void)
 {
-#ifndef CONFIG_REPLICATION
-    qmp_unregister_command(&qmp_commands, "xen-set-replication");
-    qmp_unregister_command(&qmp_commands, "query-xen-replication-status");
-    qmp_unregister_command(&qmp_commands, "xen-colo-do-checkpoint");
-#endif
 #ifndef TARGET_I386
     qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
 #endif

Same drill as for PATCH 16.

Commands you make conditional:

* xen-set-replication, query-xen-replication-status, xen-colo-do-checkpoint

Before the patch, we first register the commands unconditionally in generated code (requires a stub), then conditionally unregister in qmp_unregister_commands_hack().

Afterwards, we register only when CONFIG_REPLICATION. The command fails exactly the same, with CommandNotFound.

Improvement, because now query-qmp-schema is accurate, and we're one step close to killing qmp_unregister_commands_hack().

Check for completeness by reviewing the case insensitive occurrences of replication in the schema not covered by your patch, and review uses of CONFIG_REPLICATION for possible schema connections (I did that for CONFIG_VNC [PATCH 16] and CONFIG_SPICE [PATCH 17], too):

Quoted 8 lines

* enum BlockdevDriver value "replication" in command blockdev-add

* Makefile.objs:block-obj-$(CONFIG_REPLICATION) += replication.o block/Makefile.objs:block-obj-$(CONFIG_REPLICATION) += replication.o

I think the following should be ifdef CONFIG_REPLICATION: enum BlockdevDriver value @replication BlockdevOptions variant @replication, BlockdevOptionsReplication, BlockdevOptionsReplicationMode.

Added

Quoted 3 lines

Some of this analysis should perhaps be worked into the commit message.

Done

thanks

Signature · 1 line
Marc-André Lureau
#50 lore raw

PATCH 19/26 build-sys: move qapi variables in qapi.mak

In reply to Marc-André Lureau
---
 Makefile | 43 +++++++++++++++++--------------------------
 qapi.mak | 14 ++++++++++++++
 2 files changed, 31 insertions(+), 26 deletions(-)
 create mode 100644 qapi.mak
Makefile +17 -26
index ef721480eb..8cd30fd88e 100644
@@ -50,6 +50,7 @@ endif
 endif
 
 include $(SRC_PATH)/rules.mak
+include $(SRC_PATH)/qapi.mak
 
 GENERATED_FILES = qemu-version.h config-host.h qemu-options.def
 GENERATED_FILES += qmp-commands.h qapi-types.h qapi-visit.h qapi-event.h
@@ -390,56 +391,46 @@ qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx $(SRC_PATH)/scripts/hxtool
 qemu-ga$(EXESUF): LIBS = $(LIBS_QGA)
 qemu-ga$(EXESUF): QEMU_CFLAGS += -I qga/qapi-generated
 
-gen-out-type = $(subst .,-,$(suffix $@))
-
-qapi-py = $(SRC_PATH)/scripts/qapi.py $(SRC_PATH)/scripts/ordereddict.py
-
 qga/qapi-generated/qga-qapi-types.c qga/qapi-generated/qga-qapi-types.h :\
-$(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-types.py $(qapi-py)
+$(SRC_PATH)/qga/qapi-schema.json $(qapi-types-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py \
-		$(gen-out-type) -o qga/qapi-generated -p "qga-" $<, \
+		$(qapi-gen-type) -o qga/qapi-generated -p "qga-" $<, \
 		"GEN","$@")
 qga/qapi-generated/qga-qapi-visit.c qga/qapi-generated/qga-qapi-visit.h :\
-$(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-visit.py $(qapi-py)
+$(SRC_PATH)/qga/qapi-schema.json $(qapi-visit-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py \
-		$(gen-out-type) -o qga/qapi-generated -p "qga-" $<, \
+		$(qapi-gen-type) -o qga/qapi-generated -p "qga-" $<, \
 		"GEN","$@")
 qga/qapi-generated/qga-qmp-commands.h qga/qapi-generated/qga-qmp-marshal.c :\
-$(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py)
+$(SRC_PATH)/qga/qapi-schema.json $(qapi-commands-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py \
-		$(gen-out-type) -o qga/qapi-generated -p "qga-" $<, \
+		$(qapi-gen-type) -o qga/qapi-generated -p "qga-" $<, \
 		"GEN","$@")
 
-qapi-modules = $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/qapi/common.json \
-               $(SRC_PATH)/qapi/block.json $(SRC_PATH)/qapi/block-core.json \
-               $(SRC_PATH)/qapi/event.json $(SRC_PATH)/qapi/introspect.json \
-               $(SRC_PATH)/qapi/crypto.json $(SRC_PATH)/qapi/rocker.json \
-               $(SRC_PATH)/qapi/trace.json
-
 qapi-types.c qapi-types.h :\
-$(qapi-modules) $(SRC_PATH)/scripts/qapi-types.py $(qapi-py)
+$(qapi-modules) $(qapi-types-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py \
-		$(gen-out-type) -o "." -b $<, \
+		$(qapi-gen-type) -o "." -b $<, \
 		"GEN","$@")
 qapi-visit.c qapi-visit.h :\
-$(qapi-modules) $(SRC_PATH)/scripts/qapi-visit.py $(qapi-py)
+$(qapi-modules) $(qapi-visit-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py \
-		$(gen-out-type) -o "." -b $<, \
+		$(qapi-gen-type) -o "." -b $<, \
 		"GEN","$@")
 qapi-event.c qapi-event.h :\
-$(qapi-modules) $(SRC_PATH)/scripts/qapi-event.py $(qapi-py)
+$(qapi-modules) $(qapi-event-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-event.py \
-		$(gen-out-type) -o "." $<, \
+		$(qapi-gen-type) -o "." $<, \
 		"GEN","$@")
 qmp-commands.h qmp-marshal.c :\
-$(qapi-modules) $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py)
+$(qapi-modules) $(qapi-commands-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py \
-		$(gen-out-type) -o "." $<, \
+		$(qapi-gen-type) -o "." $<, \
 		"GEN","$@")
 qmp-introspect.h qmp-introspect.c :\
-$(qapi-modules) $(SRC_PATH)/scripts/qapi-introspect.py $(qapi-py)
+$(qapi-modules) $(qapi-introspect-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-introspect.py \
-		$(gen-out-type) -o "." $<, \
+		$(qapi-gen-type) -o "." $<, \
 		"GEN","$@")
 
 QGALIB_GEN=$(addprefix qga/qapi-generated/, qga-qapi-types.h qga-qapi-visit.h qga-qmp-commands.h)
qapi.mak new file +14 -0
new file mode 100644
index 0000000000..70196127d9
@@ -0,0 +1,14 @@
+qapi-gen-type = $(subst .,-,$(suffix $@))
+
+qapi-modules = $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/qapi/common.json \
+       $(SRC_PATH)/qapi/block.json $(SRC_PATH)/qapi/block-core.json \
+       $(SRC_PATH)/qapi/event.json $(SRC_PATH)/qapi/introspect.json \
+       $(SRC_PATH)/qapi/crypto.json $(SRC_PATH)/qapi/rocker.json \
+       $(SRC_PATH)/qapi/trace.json
+
+qapi-py = $(SRC_PATH)/scripts/qapi.py $(SRC_PATH)/scripts/ordereddict.py
+qapi-types-py = $(SRC_PATH)/scripts/qapi-types.py $(qapi-py)
+qapi-visit-py = $(SRC_PATH)/scripts/qapi-visit.py $(qapi-py)
+qapi-commands-py = $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py)
+qapi-introspect-py = $(SRC_PATH)/scripts/qapi-introspect.py $(qapi-py)
+
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#51 lore raw

Re: [Qemu-devel] [PATCH 19/26] build-sys: move qapi variables in qapi.mak

We don't move in qapi.mak we move *into* qapi.mak.

While we're talking: use a more common tag, and start the phrase with a capital letter: "Makefile: Move qapi variables into qapi.mak".

Commit message then still misses the most important part: why?

Quoted 102 lines · Marc-André Lureau
---
 Makefile | 43 +++++++++++++++++--------------------------
 qapi.mak | 14 ++++++++++++++
 2 files changed, 31 insertions(+), 26 deletions(-)
 create mode 100644 qapi.mak
Makefile +17 -26
index ef721480eb..8cd30fd88e 100644
@@ -50,6 +50,7 @@ endif
 endif
 
 include $(SRC_PATH)/rules.mak
+include $(SRC_PATH)/qapi.mak
 
 GENERATED_FILES = qemu-version.h config-host.h qemu-options.def
 GENERATED_FILES += qmp-commands.h qapi-types.h qapi-visit.h qapi-event.h
@@ -390,56 +391,46 @@ qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx $(SRC_PATH)/scripts/hxtool
 qemu-ga$(EXESUF): LIBS = $(LIBS_QGA)
 qemu-ga$(EXESUF): QEMU_CFLAGS += -I qga/qapi-generated
 
-gen-out-type = $(subst .,-,$(suffix $@))
-
-qapi-py = $(SRC_PATH)/scripts/qapi.py $(SRC_PATH)/scripts/ordereddict.py
-
 qga/qapi-generated/qga-qapi-types.c qga/qapi-generated/qga-qapi-types.h :\
-$(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-types.py $(qapi-py)
+$(SRC_PATH)/qga/qapi-schema.json $(qapi-types-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py \
-		$(gen-out-type) -o qga/qapi-generated -p "qga-" $<, \
+		$(qapi-gen-type) -o qga/qapi-generated -p "qga-" $<, \
 		"GEN","$@")
 qga/qapi-generated/qga-qapi-visit.c qga/qapi-generated/qga-qapi-visit.h :\
-$(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-visit.py $(qapi-py)
+$(SRC_PATH)/qga/qapi-schema.json $(qapi-visit-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py \
-		$(gen-out-type) -o qga/qapi-generated -p "qga-" $<, \
+		$(qapi-gen-type) -o qga/qapi-generated -p "qga-" $<, \
 		"GEN","$@")
 qga/qapi-generated/qga-qmp-commands.h qga/qapi-generated/qga-qmp-marshal.c :\
-$(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py)
+$(SRC_PATH)/qga/qapi-schema.json $(qapi-commands-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py \
-		$(gen-out-type) -o qga/qapi-generated -p "qga-" $<, \
+		$(qapi-gen-type) -o qga/qapi-generated -p "qga-" $<, \
 		"GEN","$@")
 
-qapi-modules = $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/qapi/common.json \
-               $(SRC_PATH)/qapi/block.json $(SRC_PATH)/qapi/block-core.json \
-               $(SRC_PATH)/qapi/event.json $(SRC_PATH)/qapi/introspect.json \
-               $(SRC_PATH)/qapi/crypto.json $(SRC_PATH)/qapi/rocker.json \
-               $(SRC_PATH)/qapi/trace.json
-
 qapi-types.c qapi-types.h :\
-$(qapi-modules) $(SRC_PATH)/scripts/qapi-types.py $(qapi-py)
+$(qapi-modules) $(qapi-types-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py \
-		$(gen-out-type) -o "." -b $<, \
+		$(qapi-gen-type) -o "." -b $<, \
 		"GEN","$@")
 qapi-visit.c qapi-visit.h :\
-$(qapi-modules) $(SRC_PATH)/scripts/qapi-visit.py $(qapi-py)
+$(qapi-modules) $(qapi-visit-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py \
-		$(gen-out-type) -o "." -b $<, \
+		$(qapi-gen-type) -o "." -b $<, \
 		"GEN","$@")
 qapi-event.c qapi-event.h :\
-$(qapi-modules) $(SRC_PATH)/scripts/qapi-event.py $(qapi-py)
+$(qapi-modules) $(qapi-event-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-event.py \
-		$(gen-out-type) -o "." $<, \
+		$(qapi-gen-type) -o "." $<, \
 		"GEN","$@")
 qmp-commands.h qmp-marshal.c :\
-$(qapi-modules) $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py)
+$(qapi-modules) $(qapi-commands-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py \
-		$(gen-out-type) -o "." $<, \
+		$(qapi-gen-type) -o "." $<, \
 		"GEN","$@")
 qmp-introspect.h qmp-introspect.c :\
-$(qapi-modules) $(SRC_PATH)/scripts/qapi-introspect.py $(qapi-py)
+$(qapi-modules) $(qapi-introspect-py)
 	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-introspect.py \
-		$(gen-out-type) -o "." $<, \
+		$(qapi-gen-type) -o "." $<, \
 		"GEN","$@")
 
 QGALIB_GEN=$(addprefix qga/qapi-generated/, qga-qapi-types.h qga-qapi-visit.h qga-qmp-commands.h)
qapi.mak new file +4 -0
new file mode 100644
index 0000000000..70196127d9
@@ -0,0 +1,14 @@
+qapi-gen-type = $(subst .,-,$(suffix $@))
+
+qapi-modules = $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/qapi/common.json \
+       $(SRC_PATH)/qapi/block.json $(SRC_PATH)/qapi/block-core.json \

qapi.mak line 14

Quoted 10 lines
qapi.mak new file +10 -0
+       $(SRC_PATH)/qapi/event.json $(SRC_PATH)/qapi/introspect.json \
+       $(SRC_PATH)/qapi/crypto.json $(SRC_PATH)/qapi/rocker.json \
+       $(SRC_PATH)/qapi/trace.json
+
+qapi-py = $(SRC_PATH)/scripts/qapi.py $(SRC_PATH)/scripts/ordereddict.py
+qapi-types-py = $(SRC_PATH)/scripts/qapi-types.py $(qapi-py)
+qapi-visit-py = $(SRC_PATH)/scripts/qapi-visit.py $(qapi-py)
+qapi-commands-py = $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py)
+qapi-introspect-py = $(SRC_PATH)/scripts/qapi-introspect.py $(qapi-py)
+

.git/rebase-apply/patch:111: new blank line at EOF.

I'm not sure separating the variables from the rules using them is a good idea. Perhaps I'll understand once you explain *why* you're doing this.

#52 lore raw

PATCH 20/26 tests/qmp-test: add query-qmp-schema test

In reply to Marc-André Lureau

The following patch is going to make qmp introspection per-target, and test-qobject-input-visitor.c can no longer link with qmp_schema_qlit. Use a run-time QMP test instead to validate the query-qmp-schema schema.

---
 tests/qmp-test.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
tests/qmp-test.c +21 -0
index 5d0260b2be..1fd37092b5 100644
@@ -129,11 +129,32 @@ static void test_qmp_protocol(void)
     qtest_end();
 }
 
+static void test_qmp_query_schema(void)
+{
+    SchemaInfoList *schema;
+    QDict *resp;
+    Visitor *v;
+
+    qtest_start(common_args);
+
+    resp = qmp("{'execute': 'query-qmp-schema'}");
+    v = qobject_input_visitor_new(qdict_get(resp, "return"));
+    visit_type_SchemaInfoList(v, NULL, &schema, &error_abort);
+    g_assert(schema);
+
+    qapi_free_SchemaInfoList(schema);
+    visit_free(v);
+    QDECREF(resp);
+
+    qtest_end();
+}
+
 int main(int argc, char *argv[])
 {
     g_test_init(&argc, &argv, NULL);
 
     qtest_add_func("qmp/protocol", test_qmp_protocol);
+    qtest_add_func("qmp/query-schema", test_qmp_query_schema);
 
     return g_test_run();
 }
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#53 lore raw

PATCH 21/26 build-sys: make qemu qapi objects per-target

In reply to Marc-André Lureau

The qapi schema has per-target definitions. Move qapi objects in the per-target build, so they can be configured at compile time.

Keep qapi-types.o qapi-visit.o in util-obj as they are necessary for common code, but they will be overwritten during the target link. Add some stubs for block events, in code shared by tools & qemu.

The following patch will configure the schema to conditionally remove per-target disabled features.

---
 stubs/qapi-event.c                 | 74 ++++++++++++++++++++++++++++++++++++++
 tests/test-qobject-input-visitor.c |  1 -
 Makefile.objs                      |  9 +----
 Makefile.target                    |  4 +++
 stubs/Makefile.objs                |  1 +
 trace/Makefile.objs                |  2 +-
 6 files changed, 81 insertions(+), 10 deletions(-)
 create mode 100644 stubs/qapi-event.c
stubs/qapi-event.c new file +74 -0
new file mode 100644
index 0000000000..9415299f3a
@@ -0,0 +1,74 @@
+#include "qemu/osdep.h"
+#include "qapi-event.h"
+
+void qapi_event_send_device_tray_moved(const char *device, const char *id,
+                                       bool tray_open, Error **errp)
+{
+}
+
+void qapi_event_send_quorum_report_bad(QuorumOpType type, bool has_error,
+                                       const char *error, const char *node_name,
+                                       int64_t sector_num,
+                                       int64_t sectors_count, Error **errp)
+{
+}
+
+void qapi_event_send_quorum_failure(const char *reference, int64_t sector_num,
+                                    int64_t sectors_count, Error **errp)
+{
+}
+
+void qapi_event_send_block_job_cancelled(BlockJobType type, const char *device,
+                                         int64_t len, int64_t offset,
+                                         int64_t speed, Error **errp)
+{
+}
+
+void qapi_event_send_block_job_completed(BlockJobType type, const char *device,
+                                         int64_t len, int64_t offset,
+                                         int64_t speed, bool has_error,
+                                         const char *error, Error **errp)
+{
+}
+
+void qapi_event_send_block_job_error(const char *device,
+                                     IoOperationType operation,
+                                     BlockErrorAction action, Error **errp)
+{
+}
+
+void qapi_event_send_block_job_ready(BlockJobType type, const char *device,
+                                     int64_t len, int64_t offset, int64_t speed,
+                                     Error **errp)
+{
+}
+
+void qapi_event_send_block_io_error(const char *device, const char *node_name,
+                                    IoOperationType operation,
+                                    BlockErrorAction action, bool has_nospace,
+                                    bool nospace, const char *reason,
+                                    Error **errp)
+{
+}
+
+void qapi_event_send_block_image_corrupted(const char *device,
+                                           bool has_node_name,
+                                           const char *node_name,
+                                           const char *msg, bool has_offset,
+                                           int64_t offset, bool has_size,
+                                           int64_t size, bool fatal,
+                                           Error **errp)
+{
+}
+
+void qapi_event_send_block_write_threshold(const char *node_name,
+                                           uint64_t amount_exceeded,
+                                           uint64_t write_threshold,
+                                           Error **errp)
+{
+}
+
+void qapi_event_send_device_deleted(bool has_device, const char *device,
+                                    const char *path, Error **errp)
+{
+}
tests/test-qobject-input-visitor.c +0 -1
index 4da5d02c35..0a9352c5c1 100644
@@ -1266,7 +1266,6 @@ static void test_visitor_in_qmp_introspect(TestInputVisitorData *data,
                                            const void *unused)
 {
     do_test_visitor_in_qmp_introspect(data, &test_qmp_schema_qlit);
-    do_test_visitor_in_qmp_introspect(data, &qmp_schema_qlit);
 }
 
 int main(int argc, char **argv)
Makefile.objs +1 -8
index 24a4ea08b8..2664720f9b 100644
@@ -2,7 +2,7 @@
 # Common libraries for tools and emulators
 stub-obj-y = stubs/ crypto/
 util-obj-y = util/ qobject/ qapi/
-util-obj-y += qmp-introspect.o qapi-types.o qapi-visit.o qapi-event.o
+util-obj-y += qapi-types.o qapi-visit.o
 
 chardev-obj-y = chardev/
 
@@ -72,13 +72,6 @@ common-obj-y += chardev/
 common-obj-$(CONFIG_SECCOMP) += qemu-seccomp.o
 
 common-obj-$(CONFIG_FDT) += device_tree.o
-
-######################################################################
-# qapi
-
-common-obj-y += qmp-marshal.o
-common-obj-y += qmp-introspect.o
-common-obj-y += qmp.o hmp.o
 endif
 
 #######################################################################
Makefile.target +4 -0
index 2baec9252f..a97dd056ad 100644
@@ -154,6 +154,10 @@ endif
 
 GENERATED_FILES += hmp-commands.h hmp-commands-info.h
 
+obj-y += qmp-introspect.o qapi-types.o qapi-visit.o qapi-event.o
+obj-y += qmp-marshal.o qmp-introspect.o
+obj-y += qmp.o hmp.o
+
 endif # CONFIG_SOFTMMU
 
 # Workaround for http://gcc.gnu.org/PR55489, see configure.
stubs/Makefile.objs +1 -0
index f5b47bfd74..1b2bef99c9 100644
@@ -21,6 +21,7 @@ stub-obj-y += machine-init-done.o
 stub-obj-y += migr-blocker.o
 stub-obj-y += monitor.o
 stub-obj-y += notify-event.o
+stub-obj-y += qapi-event.o
 stub-obj-y += qtest.o
 stub-obj-y += replay.o
 stub-obj-y += runstate-check.o
trace/Makefile.objs +1 -1
index afd571c3ec..6447729d60 100644
@@ -56,4 +56,4 @@ util-obj-$(CONFIG_TRACE_SIMPLE) += simple.o
 util-obj-$(CONFIG_TRACE_FTRACE) += ftrace.o
 util-obj-y += control.o
 target-obj-y += control-target.o
-util-obj-y += qmp.o
+target-obj-y += qmp.o
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#54 lore raw

Re: [Qemu-devel] [PATCH 21/26] build-sys: make qemu qapi objects per-target

Quoted 2 lines · Marc-André Lureau

The qapi schema has per-target definitions. Move qapi objects in the per-target build, so they can be configured at compile time.

Suggest something like:

QAPI can't do target-specific conditionals (the symbols are poisoned), and the work-around is to pretend the target-specific stuff is target-independent, with stubs for the other targets. Makes the target-specifity invisible in introspection.

To unpoison the symbols, we need to move the generated QAPI code to the per-target build.

Quoted 2 lines

Keep qapi-types.o qapi-visit.o in util-obj as they are necessary for common code, but they will be overwritten during the target link.

--verbose: how are they supposed to even compile in the target-independent build once we generate #if defined(TARGET_FOO) into them?

Quoted 2 lines

Add
some stubs for block events, in code shared by tools & qemu.

Sounds awkward.

Quoted 2 lines

The following patch will configure the schema to conditionally remove per-target disabled features.

"The following patches", right?

Quoted 81 lines
---
 stubs/qapi-event.c                 | 74 ++++++++++++++++++++++++++++++++++++++
 tests/test-qobject-input-visitor.c |  1 -
 Makefile.objs                      |  9 +----
 Makefile.target                    |  4 +++
 stubs/Makefile.objs                |  1 +
 trace/Makefile.objs                |  2 +-
 6 files changed, 81 insertions(+), 10 deletions(-)
 create mode 100644 stubs/qapi-event.c
stubs/qapi-event.c new file +64 -0
new file mode 100644
index 0000000000..9415299f3a
@@ -0,0 +1,74 @@
+#include "qemu/osdep.h"
+#include "qapi-event.h"
+
+void qapi_event_send_device_tray_moved(const char *device, const char *id,
+                                       bool tray_open, Error **errp)
+{
+}
+
+void qapi_event_send_quorum_report_bad(QuorumOpType type, bool has_error,
+                                       const char *error, const char *node_name,
+                                       int64_t sector_num,
+                                       int64_t sectors_count, Error **errp)
+{
+}
+
+void qapi_event_send_quorum_failure(const char *reference, int64_t sector_num,
+                                    int64_t sectors_count, Error **errp)
+{
+}
+
+void qapi_event_send_block_job_cancelled(BlockJobType type, const char *device,
+                                         int64_t len, int64_t offset,
+                                         int64_t speed, Error **errp)
+{
+}
+
+void qapi_event_send_block_job_completed(BlockJobType type, const char *device,
+                                         int64_t len, int64_t offset,
+                                         int64_t speed, bool has_error,
+                                         const char *error, Error **errp)
+{
+}
+
+void qapi_event_send_block_job_error(const char *device,
+                                     IoOperationType operation,
+                                     BlockErrorAction action, Error **errp)
+{
+}
+
+void qapi_event_send_block_job_ready(BlockJobType type, const char *device,
+                                     int64_t len, int64_t offset, int64_t speed,
+                                     Error **errp)
+{
+}
+
+void qapi_event_send_block_io_error(const char *device, const char *node_name,
+                                    IoOperationType operation,
+                                    BlockErrorAction action, bool has_nospace,
+                                    bool nospace, const char *reason,
+                                    Error **errp)
+{
+}
+
+void qapi_event_send_block_image_corrupted(const char *device,
+                                           bool has_node_name,
+                                           const char *node_name,
+                                           const char *msg, bool has_offset,
+                                           int64_t offset, bool has_size,
+                                           int64_t size, bool fatal,
+                                           Error **errp)
+{
+}
+
+void qapi_event_send_block_write_threshold(const char *node_name,

stubs/qapi-event.c line 74

Quoted 10 lines
stubs/qapi-event.c new file +10 -0
+                                           uint64_t amount_exceeded,
+                                           uint64_t write_threshold,
+                                           Error **errp)
+{
+}
+
+void qapi_event_send_device_deleted(bool has_device, const char *device,
+                                    const char *path, Error **errp)
+{
+}

Yup, awkward.

tests/test-qobject-input-visitor.c static void test_visitor_in_qmp_introspect(TestInputVisitorData *data, · line 1271

Quoted 12 lines
tests/test-qobject-input-visitor.c +0 -1
index 4da5d02c35..0a9352c5c1 100644
@@ -1266,7 +1266,6 @@ static void test_visitor_in_qmp_introspect(TestInputVisitorData *data,
                                            const void *unused)
 {
     do_test_visitor_in_qmp_introspect(data, &test_qmp_schema_qlit);
-    do_test_visitor_in_qmp_introspect(data, &qmp_schema_qlit);
 }
 
 int main(int argc, char **argv)

Either squash this change into PATCH 20, or mention it in the commit message.

See also my review of PATCH 04.

Quoted 32 lines
Makefile.objs +1 -8
index 24a4ea08b8..2664720f9b 100644
@@ -2,7 +2,7 @@
 # Common libraries for tools and emulators
 stub-obj-y = stubs/ crypto/
 util-obj-y = util/ qobject/ qapi/
-util-obj-y += qmp-introspect.o qapi-types.o qapi-visit.o qapi-event.o
+util-obj-y += qapi-types.o qapi-visit.o
 
 chardev-obj-y = chardev/
 
@@ -72,13 +72,6 @@ common-obj-y += chardev/
 common-obj-$(CONFIG_SECCOMP) += qemu-seccomp.o
 
 common-obj-$(CONFIG_FDT) += device_tree.o
-
-######################################################################
-# qapi
-
-common-obj-y += qmp-marshal.o
-common-obj-y += qmp-introspect.o
-common-obj-y += qmp.o hmp.o
 endif
 
 #######################################################################
Makefile.target +0 -0
index 2baec9252f..a97dd056ad 100644

Makefile.target endif · line 160

Quoted 7 lines
Makefile.target +4 -0
@@ -154,6 +154,10 @@ endif
 
 GENERATED_FILES += hmp-commands.h hmp-commands-info.h
 
+obj-y += qmp-introspect.o qapi-types.o qapi-visit.o qapi-event.o
+obj-y += qmp-marshal.o qmp-introspect.o
+obj-y += qmp.o hmp.o
+

Moving so much code from "compile once" to "compile per target" is kind of sad. With the full series applied, I see

    $ wc qapi*c qmp*c
       1528    3089   34576 qapi-event.c
       5097    9126  107004 qapi-types.c
      18848   44862  469514 qapi-visit.c
      12407   32395  404704 qmp-introspect.c
       6883   14997  182063 qmp-marshal.c
      44763  104469 1197861 total

Is there any way to split stuff so we recompile less? Note that this is a valid question even without your patches: changing one little thing in the QAPI schema commonly triggers a lengthy recompile. In large part because our undisciplined use of #include. But also because the generator's output is monolithic.

I'm not expecting you to answer this question now, I just want to toss it out :)

Quoted 25 lines
Makefile.target +0 -0
 endif # CONFIG_SOFTMMU
 
 # Workaround for http://gcc.gnu.org/PR55489, see configure.
stubs/Makefile.objs +1 -0
index f5b47bfd74..1b2bef99c9 100644
@@ -21,6 +21,7 @@ stub-obj-y += machine-init-done.o
 stub-obj-y += migr-blocker.o
 stub-obj-y += monitor.o
 stub-obj-y += notify-event.o
+stub-obj-y += qapi-event.o
 stub-obj-y += qtest.o
 stub-obj-y += replay.o
 stub-obj-y += runstate-check.o
trace/Makefile.objs +1 -1
index afd571c3ec..6447729d60 100644
@@ -56,4 +56,4 @@ util-obj-$(CONFIG_TRACE_SIMPLE) += simple.o
 util-obj-$(CONFIG_TRACE_FTRACE) += ftrace.o
 util-obj-y += control.o
 target-obj-y += control-target.o
-util-obj-y += qmp.o
+target-obj-y += qmp.o
#55 lore raw

Re: [Qemu-devel] [PATCH 21/26] build-sys: make qemu qapi objects per-target

Hi

Quoted 15 lines · Markus Armbruster
Quoted 2 lines · Marc-André Lureau

The qapi schema has per-target definitions. Move qapi objects in the per-target build, so they can be configured at compile time.

Suggest something like:

QAPI can't do target-specific conditionals (the symbols are poisoned), and the work-around is to pretend the target-specific stuff is target-independent, with stubs for the other targets. Makes the target-specifity invisible in introspection.

To unpoison the symbols, we need to move the generated QAPI code to the per-target build.

Quoted 7 lines
Quoted 2 lines

Keep qapi-types.o qapi-visit.o in util-obj as they are necessary for common code, but they will be overwritten during the target link.

--verbose: how are they supposed to even compile in the target-independent build once we generate #if defined(TARGET_FOO) into them?

For common code, they will compile without TARGET_FOO.

Quoted 4 lines
Quoted 2 lines

Add
some stubs for block events, in code shared by tools & qemu.

Sounds awkward.

I guess the alternative is to make the objects that depend on it per-target.

Quoted 5 lines
Quoted 3 lines

The following patch will configure the schema to conditionally remove per-target disabled features.

"The following patches", right?

yes, fixed

Quoted 106 lines
Quoted 82 lines
---
 stubs/qapi-event.c                 | 74 ++++++++++++++++++++++++++++++++++++++
 tests/test-qobject-input-visitor.c |  1 -
 Makefile.objs                      |  9 +----
 Makefile.target                    |  4 +++
 stubs/Makefile.objs                |  1 +
 trace/Makefile.objs                |  2 +-
 6 files changed, 81 insertions(+), 10 deletions(-)
 create mode 100644 stubs/qapi-event.c
stubs/qapi-event.c new file +64 -0
new file mode 100644
index 0000000000..9415299f3a
@@ -0,0 +1,74 @@
+#include "qemu/osdep.h"
+#include "qapi-event.h"
+
+void qapi_event_send_device_tray_moved(const char *device, const char *id,
+                                       bool tray_open, Error **errp)
+{
+}
+
+void qapi_event_send_quorum_report_bad(QuorumOpType type, bool has_error,
+                                       const char *error, const char *node_name,
+                                       int64_t sector_num,
+                                       int64_t sectors_count, Error **errp)
+{
+}
+
+void qapi_event_send_quorum_failure(const char *reference, int64_t sector_num,
+                                    int64_t sectors_count, Error **errp)
+{
+}
+
+void qapi_event_send_block_job_cancelled(BlockJobType type, const char *device,
+                                         int64_t len, int64_t offset,
+                                         int64_t speed, Error **errp)
+{
+}
+
+void qapi_event_send_block_job_completed(BlockJobType type, const char *device,
+                                         int64_t len, int64_t offset,
+                                         int64_t speed, bool has_error,
+                                         const char *error, Error **errp)
+{
+}
+
+void qapi_event_send_block_job_error(const char *device,
+                                     IoOperationType operation,
+                                     BlockErrorAction action, Error **errp)
+{
+}
+
+void qapi_event_send_block_job_ready(BlockJobType type, const char *device,
+                                     int64_t len, int64_t offset, int64_t speed,
+                                     Error **errp)
+{
+}
+
+void qapi_event_send_block_io_error(const char *device, const char *node_name,
+                                    IoOperationType operation,
+                                    BlockErrorAction action, bool has_nospace,
+                                    bool nospace, const char *reason,
+                                    Error **errp)
+{
+}
+
+void qapi_event_send_block_image_corrupted(const char *device,
+                                           bool has_node_name,
+                                           const char *node_name,
+                                           const char *msg, bool has_offset,
+                                           int64_t offset, bool has_size,
+                                           int64_t size, bool fatal,
+                                           Error **errp)
+{
+}
+
+void qapi_event_send_block_write_threshold(const char *node_name,

stubs/qapi-event.c line 74

Quoted 10 lines
stubs/qapi-event.c new file +10 -0
+                                           uint64_t amount_exceeded,
+                                           uint64_t write_threshold,
+                                           Error **errp)
+{
+}
+
+void qapi_event_send_device_deleted(bool has_device, const char *device,
+                                    const char *path, Error **errp)
+{
+}

Yup, awkward.

Quoted 11 lines
tests/test-qobject-input-visitor.c +0 -1
index 4da5d02c35..0a9352c5c1 100644
@@ -1266,7 +1266,6 @@ static void test_visitor_in_qmp_introspect(TestInputVisitorData *data,
                                            const void *unused)
 {
     do_test_visitor_in_qmp_introspect(data, &test_qmp_schema_qlit);
-    do_test_visitor_in_qmp_introspect(data, &qmp_schema_qlit);
 }
 
Quoted 4 lines

tests/test-qobject-input-visitor.c static void test_visitor_in_qmp_introspect(TestInputVisitorData *data, · line 1271

Quoted 1 line
tests/test-qobject-input-visitor.c +0 -0
 int main(int argc, char **argv)

Either squash this change into PATCH 20, or mention it in the commit message.

done

Quoted 54 lines

See also my review of PATCH 04.

Quoted 32 lines
Makefile.objs +1 -8
index 24a4ea08b8..2664720f9b 100644
@@ -2,7 +2,7 @@
 # Common libraries for tools and emulators
 stub-obj-y = stubs/ crypto/
 util-obj-y = util/ qobject/ qapi/
-util-obj-y += qmp-introspect.o qapi-types.o qapi-visit.o qapi-event.o
+util-obj-y += qapi-types.o qapi-visit.o
 
 chardev-obj-y = chardev/
 
@@ -72,13 +72,6 @@ common-obj-y += chardev/
 common-obj-$(CONFIG_SECCOMP) += qemu-seccomp.o
 
 common-obj-$(CONFIG_FDT) += device_tree.o
-
-######################################################################
-# qapi
-
-common-obj-y += qmp-marshal.o
-common-obj-y += qmp-introspect.o
-common-obj-y += qmp.o hmp.o
 endif
 
 #######################################################################
Makefile.target +0 -0
index 2baec9252f..a97dd056ad 100644

Makefile.target endif · line 160

Quoted 7 lines
Makefile.target +4 -0
@@ -154,6 +154,10 @@ endif
 
 GENERATED_FILES += hmp-commands.h hmp-commands-info.h
 
+obj-y += qmp-introspect.o qapi-types.o qapi-visit.o qapi-event.o
+obj-y += qmp-marshal.o qmp-introspect.o
+obj-y += qmp.o hmp.o
+

Moving so much code from "compile once" to "compile per target" is kind of sad. With the full series applied, I see

    $ wc qapi*c qmp*c
       1528    3089   34576 qapi-event.c
       5097    9126  107004 qapi-types.c
      18848   44862  469514 qapi-visit.c
      12407   32395  404704 qmp-introspect.c
       6883   14997  182063 qmp-marshal.c
      44763  104469 1197861 total
Quoted 9 lines

Is there any way to split stuff so we recompile less? Note that this is a valid question even without your patches: changing one little thing in the QAPI schema commonly triggers a lengthy recompile. In large part because our undisciplined use of #include. But also because the generator's output is monolithic.

I'm not expecting you to answer this question now, I just want to toss it out :)

Yes, I also think it's the next logical thing to do. We would benefit from a modular schema, especially common/target split.

Quoted 26 lines
Quoted 25 lines
Makefile.target +0 -0
 endif # CONFIG_SOFTMMU
 
 # Workaround for http://gcc.gnu.org/PR55489, see configure.
stubs/Makefile.objs +1 -0
index f5b47bfd74..1b2bef99c9 100644
@@ -21,6 +21,7 @@ stub-obj-y += machine-init-done.o
 stub-obj-y += migr-blocker.o
 stub-obj-y += monitor.o
 stub-obj-y += notify-event.o
+stub-obj-y += qapi-event.o
 stub-obj-y += qtest.o
 stub-obj-y += replay.o
 stub-obj-y += runstate-check.o
trace/Makefile.objs +1 -1
index afd571c3ec..6447729d60 100644
@@ -56,4 +56,4 @@ util-obj-$(CONFIG_TRACE_SIMPLE) += simple.o
 util-obj-$(CONFIG_TRACE_FTRACE) += ftrace.o
 util-obj-y += control.o
 target-obj-y += control-target.o
-util-obj-y += qmp.o
+target-obj-y += qmp.o
Signature · 1 line
Marc-André Lureau
#56 lore raw

PATCH 22/26 qapi: make rtc-reset-reinjection depend on TARGET_I386

In reply to Marc-André Lureau
---
 qapi-schema.json |  3 ++-
 monitor.c        | 10 ----------
 2 files changed, 2 insertions(+), 11 deletions(-)
qapi-schema.json +2 -1
index 2f4528c769..2361c13fc8 100644
@@ -6270,7 +6270,8 @@
 # <- { "return": {} }
 #
 ##
-{ 'command': 'rtc-reset-reinjection' }
+{ 'command': 'rtc-reset-reinjection',
+  'if': ['defined(NEED_CPU_H)', 'defined(TARGET_I386)'] }
 
 # Rocker ethernet network switch
 { 'include': 'qapi/rocker.json' }
monitor.c +0 -10
index 383c84d162..f3dafafa22 100644
@@ -970,9 +970,6 @@ static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
  */
 static void qmp_unregister_commands_hack(void)
 {
-#ifndef TARGET_I386
-    qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
-#endif
 #ifndef TARGET_S390X
     qmp_unregister_command(&qmp_commands, "dump-skeys");
 #endif
@@ -4151,13 +4148,6 @@ QemuOptsList qemu_mon_opts = {
     },
 };
 
-#ifndef TARGET_I386
-void qmp_rtc_reset_reinjection(Error **errp)
-{
-    error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
-}
-#endif
-
 #ifndef TARGET_S390X
 void qmp_dump_skeys(const char *filename, Error **errp)
 {
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#57 lore raw

Re: [Qemu-devel] [PATCH 22/26] qapi: make rtc-reset-reinjection depend on TARGET_I386

Quoted 11 lines · Marc-André Lureau
---
 qapi-schema.json |  3 ++-
 monitor.c        | 10 ----------
 2 files changed, 2 insertions(+), 11 deletions(-)
qapi-schema.json +0 -0
index 2f4528c769..2361c13fc8 100644

qapi-schema.json line 6274

Quoted 6 lines
qapi-schema.json +2 -1
@@ -6270,7 +6270,8 @@
 # <- { "return": {} }
 #
 ##
-{ 'command': 'rtc-reset-reinjection' }
+{ 'command': 'rtc-reset-reinjection',
+  'if': ['defined(NEED_CPU_H)', 'defined(TARGET_I386)'] }

Aha, here' you use the list syntax.

And your strategy to keep things compiling also becomes clear: you wrap uses of poisoned symbols like TARGET_I386 in #if defined(NEED_CPU_H).

Not exactly elegant, but looks workable. But you need to explain this solution in commit messages [PATCH 21, I guess] and document it in qapi-code-gen.txt. *Unless* we can find a better one.

Quoted 31 lines
qapi-schema.json +0 -0
 # Rocker ethernet network switch
 { 'include': 'qapi/rocker.json' }
monitor.c +0 -10
index 383c84d162..f3dafafa22 100644
@@ -970,9 +970,6 @@ static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
  */
 static void qmp_unregister_commands_hack(void)
 {
-#ifndef TARGET_I386
-    qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
-#endif
 #ifndef TARGET_S390X
     qmp_unregister_command(&qmp_commands, "dump-skeys");
 #endif
@@ -4151,13 +4148,6 @@ QemuOptsList qemu_mon_opts = {
     },
 };
 
-#ifndef TARGET_I386
-void qmp_rtc_reset_reinjection(Error **errp)
-{
-    error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
-}
-#endif
-
 #ifndef TARGET_S390X
 void qmp_dump_skeys(const char *filename, Error **errp)
 {
#58 lore raw

Re: [Qemu-devel] [PATCH 22/26] qapi: make rtc-reset-reinjection depend on TARGET_I386

Hi

Quoted 22 lines · Markus Armbruster
Quoted 11 lines · Marc-André Lureau
---
 qapi-schema.json |  3 ++-
 monitor.c        | 10 ----------
 2 files changed, 2 insertions(+), 11 deletions(-)
qapi-schema.json +0 -0
index 2f4528c769..2361c13fc8 100644

qapi-schema.json line 6274

Quoted 6 lines
qapi-schema.json +2 -1
@@ -6270,7 +6270,8 @@
 # <- { "return": {} }
 #
 ##
-{ 'command': 'rtc-reset-reinjection' }
+{ 'command': 'rtc-reset-reinjection',
+  'if': ['defined(NEED_CPU_H)', 'defined(TARGET_I386)'] }

Aha, here' you use the list syntax.

Quoted 6 lines

And your strategy to keep things compiling also becomes clear: you wrap uses of poisoned symbols like TARGET_I386 in #if defined(NEED_CPU_H).

Not exactly elegant, but looks workable. But you need to explain this solution in commit messages [PATCH 21, I guess] and document it in qapi-code-gen.txt. *Unless* we can find a better one.

Updated doc

Quoted 33 lines
Quoted 32 lines
qapi-schema.json +0 -0
 # Rocker ethernet network switch
 { 'include': 'qapi/rocker.json' }
monitor.c +0 -10
index 383c84d162..f3dafafa22 100644
@@ -970,9 +970,6 @@ static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
  */
 static void qmp_unregister_commands_hack(void)
 {
-#ifndef TARGET_I386
-    qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
-#endif
 #ifndef TARGET_S390X
     qmp_unregister_command(&qmp_commands, "dump-skeys");
 #endif
@@ -4151,13 +4148,6 @@ QemuOptsList qemu_mon_opts = {
     },
 };
 
-#ifndef TARGET_I386
-void qmp_rtc_reset_reinjection(Error **errp)
-{
-    error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
-}
-#endif
-
 #ifndef TARGET_S390X
 void qmp_dump_skeys(const char *filename, Error **errp)
 {
Signature · 1 line
Marc-André Lureau
#59 lore raw

PATCH 23/26 qapi: make s390 commands depend on TARGET_S390X

In reply to Marc-André Lureau
---
 qapi-schema.json                        | 10 +++++++---
 include/sysemu/arch_init.h              |  6 ------
 monitor.c                               | 14 --------------
 qmp.c                                   | 14 --------------
 stubs/arch-query-cpu-model-baseline.c   | 12 ------------
 stubs/arch-query-cpu-model-comparison.c | 12 ------------
 target/s390x/cpu_models.c               |  4 ++--
 stubs/Makefile.objs                     |  2 --
 8 files changed, 9 insertions(+), 65 deletions(-)
 delete mode 100644 stubs/arch-query-cpu-model-baseline.c
 delete mode 100644 stubs/arch-query-cpu-model-comparison.c
qapi-schema.json +7 -3
index 2361c13fc8..278d7e2aa3 100644
@@ -3577,7 +3577,8 @@
 #
 ##
 { 'command': 'dump-skeys',
-  'data': { 'filename': 'str' } }
+  'data': { 'filename': 'str' },
+  'if': ['defined(NEED_CPU_H)', 'defined(TARGET_S390X)']}
 
 ##
 # @netdev_add:
@@ -4621,7 +4622,9 @@
 ##
 { 'command': 'query-cpu-model-comparison',
   'data': { 'modela': 'CpuModelInfo', 'modelb': 'CpuModelInfo' },
-  'returns': 'CpuModelCompareInfo' }
+  'returns': 'CpuModelCompareInfo',
+  'if': ['defined(NEED_CPU_H)', 'defined(TARGET_S390X)']}
+
 
 ##
 # @CpuModelBaselineInfo:
@@ -4673,7 +4676,8 @@
 { 'command': 'query-cpu-model-baseline',
   'data': { 'modela': 'CpuModelInfo',
             'modelb': 'CpuModelInfo' },
-  'returns': 'CpuModelBaselineInfo' }
+  'returns': 'CpuModelBaselineInfo',
+  'if': ['defined(NEED_CPU_H)', 'defined(TARGET_S390X)']}
 
 ##
 # @AddfdInfo:
include/sysemu/arch_init.h +0 -6
index 8751c468ed..e9f1ea0cca 100644
@@ -35,11 +35,5 @@ CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp);
 CpuModelExpansionInfo *arch_query_cpu_model_expansion(CpuModelExpansionType type,
                                                       CpuModelInfo *mode,
                                                       Error **errp);
-CpuModelCompareInfo *arch_query_cpu_model_comparison(CpuModelInfo *modela,
-                                                     CpuModelInfo *modelb,
-                                                     Error **errp);
-CpuModelBaselineInfo *arch_query_cpu_model_baseline(CpuModelInfo *modela,
-                                                    CpuModelInfo *modelb,
-                                                    Error **errp);
 
 #endif
monitor.c +0 -14
index f3dafafa22..505ee5c58d 100644
@@ -970,19 +970,12 @@ static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
  */
 static void qmp_unregister_commands_hack(void)
 {
-#ifndef TARGET_S390X
-    qmp_unregister_command(&qmp_commands, "dump-skeys");
-#endif
 #ifndef TARGET_ARM
     qmp_unregister_command(&qmp_commands, "query-gic-capabilities");
 #endif
 #if !defined(TARGET_S390X) && !defined(TARGET_I386)
     qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion");
 #endif
-#if !defined(TARGET_S390X)
-    qmp_unregister_command(&qmp_commands, "query-cpu-model-baseline");
-    qmp_unregister_command(&qmp_commands, "query-cpu-model-comparison");
-#endif
 #if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
     && !defined(TARGET_S390X)
     qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
@@ -4148,13 +4141,6 @@ QemuOptsList qemu_mon_opts = {
     },
 };
 
-#ifndef TARGET_S390X
-void qmp_dump_skeys(const char *filename, Error **errp)
-{
-    error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
-}
-#endif
-
 #ifndef TARGET_ARM
 GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
 {
qmp.c +0 -14
index 90816ba283..7b6861846f 100644
@@ -553,20 +553,6 @@ CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
     return arch_query_cpu_model_expansion(type, model, errp);
 }
 
-CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *modela,
-                                                    CpuModelInfo *modelb,
-                                                    Error **errp)
-{
-    return arch_query_cpu_model_comparison(modela, modelb, errp);
-}
-
-CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *modela,
-                                                   CpuModelInfo *modelb,
-                                                   Error **errp)
-{
-    return arch_query_cpu_model_baseline(modela, modelb, errp);
-}
-
 void qmp_add_client(const char *protocol, const char *fdname,
                     bool has_skipauth, bool skipauth, bool has_tls, bool tls,
                     Error **errp)
stubs/arch-query-cpu-model-baseline.c deleted +0 -12
deleted file mode 100644
index 094ec13c2c..0000000000
@@ -1,12 +0,0 @@
-#include "qemu/osdep.h"
-#include "qemu-common.h"
-#include "sysemu/arch_init.h"
-#include "qapi/qmp/qerror.h"
-
-CpuModelBaselineInfo *arch_query_cpu_model_baseline(CpuModelInfo *modela,
-                                                    CpuModelInfo *modelb,
-                                                    Error **errp)
-{
-    error_setg(errp, QERR_UNSUPPORTED);
-    return NULL;
-}
stubs/arch-query-cpu-model-comparison.c deleted +0 -12
deleted file mode 100644
index d5486ae980..0000000000
@@ -1,12 +0,0 @@
-#include "qemu/osdep.h"
-#include "qemu-common.h"
-#include "sysemu/arch_init.h"
-#include "qapi/qmp/qerror.h"
-
-CpuModelCompareInfo *arch_query_cpu_model_comparison(CpuModelInfo *modela,
-                                                     CpuModelInfo *modelb,
-                                                     Error **errp)
-{
-    error_setg(errp, QERR_UNSUPPORTED);
-    return NULL;
-}
target/s390x/cpu_models.c +2 -2
index fa1338fc72..cdff9cdd3b 100644
@@ -559,7 +559,7 @@ static void list_add_feat(const char *name, void *opaque)
     *last = entry;
 }
 
-CpuModelCompareInfo *arch_query_cpu_model_comparison(CpuModelInfo *infoa,
+CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *infoa,
                                                      CpuModelInfo *infob,
                                                      Error **errp)
 {
@@ -632,7 +632,7 @@ CpuModelCompareInfo *arch_query_cpu_model_comparison(CpuModelInfo *infoa,
     return compare_info;
 }
 
-CpuModelBaselineInfo *arch_query_cpu_model_baseline(CpuModelInfo *infoa,
+CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *infoa,
                                                     CpuModelInfo *infob,
                                                     Error **errp)
 {
stubs/Makefile.objs +0 -2
index 1b2bef99c9..049d389966 100644
@@ -1,7 +1,5 @@
 stub-obj-y += arch-query-cpu-def.o
 stub-obj-y += arch-query-cpu-model-expansion.o
-stub-obj-y += arch-query-cpu-model-comparison.o
-stub-obj-y += arch-query-cpu-model-baseline.o
 stub-obj-y += bdrv-next-monitor-owned.o
 stub-obj-y += blk-commit-all.o
 stub-obj-y += blockdev-close-all-bdrv-states.o
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#60 lore raw

Re: [Qemu-devel] [PATCH 23/26] qapi: make s390 commands depend on TARGET_S390X

Quoted 19 lines · Marc-André Lureau
---
 qapi-schema.json                        | 10 +++++++---
 include/sysemu/arch_init.h              |  6 ------
 monitor.c                               | 14 --------------
 qmp.c                                   | 14 --------------
 stubs/arch-query-cpu-model-baseline.c   | 12 ------------
 stubs/arch-query-cpu-model-comparison.c | 12 ------------
 target/s390x/cpu_models.c               |  4 ++--
 stubs/Makefile.objs                     |  2 --
 8 files changed, 9 insertions(+), 65 deletions(-)
 delete mode 100644 stubs/arch-query-cpu-model-baseline.c
 delete mode 100644 stubs/arch-query-cpu-model-comparison.c
qapi-schema.json +0 -0
index 2361c13fc8..278d7e2aa3 100644

qapi-schema.json line 3581

Quoted 6 lines
qapi-schema.json +2 -1
@@ -3577,7 +3577,8 @@
 #
 ##
 { 'command': 'dump-skeys',
-  'data': { 'filename': 'str' } }
+  'data': { 'filename': 'str' },
+  'if': ['defined(NEED_CPU_H)', 'defined(TARGET_S390X)']}

Same technique as for TARGET_I386 in PATCH 22. See my review of it for how it works.

qapi-schema.json line 4624

Quoted 7 lines
qapi-schema.json +0 -0
 ##
 # @netdev_add:
@@ -4621,7 +4622,9 @@
 ##
 { 'command': 'query-cpu-model-comparison',
   'data': { 'modela': 'CpuModelInfo', 'modelb': 'CpuModelInfo' },

Not your patch's fault: 'modela' sounds like the name of some high end escort service or something. The QAPI naming conventions want proper words connected with dashes: 'model-a' and 'model-b'. Although I wouldn't mind just 'a' and 'b'. This should've been caught in review. Too late to fix now, I'm afraid.

Quoted 166 lines
qapi-schema.json +5 -2
-  'returns': 'CpuModelCompareInfo' }
+  'returns': 'CpuModelCompareInfo',
+  'if': ['defined(NEED_CPU_H)', 'defined(TARGET_S390X)']}
+
 
 ##
 # @CpuModelBaselineInfo:
@@ -4673,7 +4676,8 @@
 { 'command': 'query-cpu-model-baseline',
   'data': { 'modela': 'CpuModelInfo',
             'modelb': 'CpuModelInfo' },
-  'returns': 'CpuModelBaselineInfo' }
+  'returns': 'CpuModelBaselineInfo',
+  'if': ['defined(NEED_CPU_H)', 'defined(TARGET_S390X)']}
 
 ##
 # @AddfdInfo:
include/sysemu/arch_init.h +0 -6
index 8751c468ed..e9f1ea0cca 100644
@@ -35,11 +35,5 @@ CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp);
 CpuModelExpansionInfo *arch_query_cpu_model_expansion(CpuModelExpansionType type,
                                                       CpuModelInfo *mode,
                                                       Error **errp);
-CpuModelCompareInfo *arch_query_cpu_model_comparison(CpuModelInfo *modela,
-                                                     CpuModelInfo *modelb,
-                                                     Error **errp);
-CpuModelBaselineInfo *arch_query_cpu_model_baseline(CpuModelInfo *modela,
-                                                    CpuModelInfo *modelb,
-                                                    Error **errp);
 
 #endif
monitor.c +0 -14
index f3dafafa22..505ee5c58d 100644
@@ -970,19 +970,12 @@ static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
  */
 static void qmp_unregister_commands_hack(void)
 {
-#ifndef TARGET_S390X
-    qmp_unregister_command(&qmp_commands, "dump-skeys");
-#endif
 #ifndef TARGET_ARM
     qmp_unregister_command(&qmp_commands, "query-gic-capabilities");
 #endif
 #if !defined(TARGET_S390X) && !defined(TARGET_I386)
     qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion");
 #endif
-#if !defined(TARGET_S390X)
-    qmp_unregister_command(&qmp_commands, "query-cpu-model-baseline");
-    qmp_unregister_command(&qmp_commands, "query-cpu-model-comparison");
-#endif
 #if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
     && !defined(TARGET_S390X)
     qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
@@ -4148,13 +4141,6 @@ QemuOptsList qemu_mon_opts = {
     },
 };
 
-#ifndef TARGET_S390X
-void qmp_dump_skeys(const char *filename, Error **errp)
-{
-    error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
-}
-#endif
-
 #ifndef TARGET_ARM
 GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
 {
qmp.c +0 -14
index 90816ba283..7b6861846f 100644
@@ -553,20 +553,6 @@ CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
     return arch_query_cpu_model_expansion(type, model, errp);
 }
 
-CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *modela,
-                                                    CpuModelInfo *modelb,
-                                                    Error **errp)
-{
-    return arch_query_cpu_model_comparison(modela, modelb, errp);
-}
-
-CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *modela,
-                                                   CpuModelInfo *modelb,
-                                                   Error **errp)
-{
-    return arch_query_cpu_model_baseline(modela, modelb, errp);
-}
-
 void qmp_add_client(const char *protocol, const char *fdname,
                     bool has_skipauth, bool skipauth, bool has_tls, bool tls,
                     Error **errp)
stubs/arch-query-cpu-model-baseline.c deleted +0 -12
deleted file mode 100644
index 094ec13c2c..0000000000
@@ -1,12 +0,0 @@
-#include "qemu/osdep.h"
-#include "qemu-common.h"
-#include "sysemu/arch_init.h"
-#include "qapi/qmp/qerror.h"
-
-CpuModelBaselineInfo *arch_query_cpu_model_baseline(CpuModelInfo *modela,
-                                                    CpuModelInfo *modelb,
-                                                    Error **errp)
-{
-    error_setg(errp, QERR_UNSUPPORTED);
-    return NULL;
-}
stubs/arch-query-cpu-model-comparison.c deleted +0 -12
deleted file mode 100644
index d5486ae980..0000000000
@@ -1,12 +0,0 @@
-#include "qemu/osdep.h"
-#include "qemu-common.h"
-#include "sysemu/arch_init.h"
-#include "qapi/qmp/qerror.h"
-
-CpuModelCompareInfo *arch_query_cpu_model_comparison(CpuModelInfo *modela,
-                                                     CpuModelInfo *modelb,
-                                                     Error **errp)
-{
-    error_setg(errp, QERR_UNSUPPORTED);
-    return NULL;
-}
target/s390x/cpu_models.c +2 -2
index fa1338fc72..cdff9cdd3b 100644
@@ -559,7 +559,7 @@ static void list_add_feat(const char *name, void *opaque)
     *last = entry;
 }
 
-CpuModelCompareInfo *arch_query_cpu_model_comparison(CpuModelInfo *infoa,
+CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *infoa,
                                                      CpuModelInfo *infob,
                                                      Error **errp)
 {
@@ -632,7 +632,7 @@ CpuModelCompareInfo *arch_query_cpu_model_comparison(CpuModelInfo *infoa,
     return compare_info;
 }
 
-CpuModelBaselineInfo *arch_query_cpu_model_baseline(CpuModelInfo *infoa,
+CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *infoa,
                                                     CpuModelInfo *infob,
                                                     Error **errp)
 {
stubs/Makefile.objs +0 -2
index 1b2bef99c9..049d389966 100644
@@ -1,7 +1,5 @@
 stub-obj-y += arch-query-cpu-def.o
 stub-obj-y += arch-query-cpu-model-expansion.o
-stub-obj-y += arch-query-cpu-model-comparison.o
-stub-obj-y += arch-query-cpu-model-baseline.o
 stub-obj-y += bdrv-next-monitor-owned.o
 stub-obj-y += blk-commit-all.o
 stub-obj-y += blockdev-close-all-bdrv-states.o
#61 lore raw

PATCH 24/26 qapi: make query-gic-capabilities depend on TARGET_ARM

In reply to Marc-André Lureau
---
 qapi-schema.json |  3 ++-
 monitor.c        | 11 -----------
 2 files changed, 2 insertions(+), 12 deletions(-)
qapi-schema.json +2 -1
index 278d7e2aa3..a5c28bc9ad 100644
@@ -6437,7 +6437,8 @@
 #                 { "version": 3, "emulated": false, "kernel": true } ] }
 #
 ##
-{ 'command': 'query-gic-capabilities', 'returns': ['GICCapability'] }
+{ 'command': 'query-gic-capabilities', 'returns': ['GICCapability'],
+  'if': ['defined(NEED_CPU_H)', 'defined(TARGET_ARM)']}
 
 ##
 # @CpuInstanceProperties:
monitor.c +0 -11
index 505ee5c58d..3bdae8d9d0 100644
@@ -970,9 +970,6 @@ static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
  */
 static void qmp_unregister_commands_hack(void)
 {
-#ifndef TARGET_ARM
-    qmp_unregister_command(&qmp_commands, "query-gic-capabilities");
-#endif
 #if !defined(TARGET_S390X) && !defined(TARGET_I386)
     qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion");
 #endif
@@ -4141,14 +4138,6 @@ QemuOptsList qemu_mon_opts = {
     },
 };
 
-#ifndef TARGET_ARM
-GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
-{
-    error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
-    return NULL;
-}
-#endif
-
 HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
 {
     MachineState *ms = MACHINE(qdev_get_machine());
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#62 lore raw

PATCH 25/26 qapi: make query-cpu-model-expansion depend on s390 or x86

In reply to Marc-André Lureau
---
 qapi-schema.json                       |  4 +++-
 include/sysemu/arch_init.h             |  3 ---
 monitor.c                              |  3 ---
 qmp.c                                  |  7 -------
 stubs/arch-query-cpu-model-expansion.c | 12 ------------
 target/i386/cpu.c                      |  2 +-
 target/s390x/cpu_models.c              |  3 ++-
 stubs/Makefile.objs                    |  1 -
 8 files changed, 6 insertions(+), 29 deletions(-)
 delete mode 100644 stubs/arch-query-cpu-model-expansion.c
qapi-schema.json +3 -1
index a5c28bc9ad..f5e1acff83 100644
@@ -4535,7 +4535,9 @@
 { 'command': 'query-cpu-model-expansion',
   'data': { 'type': 'CpuModelExpansionType',
             'model': 'CpuModelInfo' },
-  'returns': 'CpuModelExpansionInfo' }
+  'returns': 'CpuModelExpansionInfo',
+  'if': ['defined(NEED_CPU_H)',
+         'defined(TARGET_S390X) || defined(TARGET_I386)']}
 
 ##
 # @CpuModelCompareResult:
include/sysemu/arch_init.h +0 -3
index e9f1ea0cca..fb3d20a1b8 100644
@@ -32,8 +32,5 @@ int kvm_available(void);
 int xen_available(void);
 
 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp);
-CpuModelExpansionInfo *arch_query_cpu_model_expansion(CpuModelExpansionType type,
-                                                      CpuModelInfo *mode,
-                                                      Error **errp);
 
 #endif
monitor.c +0 -3
index 3bdae8d9d0..b134c39144 100644
@@ -970,9 +970,6 @@ static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
  */
 static void qmp_unregister_commands_hack(void)
 {
-#if !defined(TARGET_S390X) && !defined(TARGET_I386)
-    qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion");
-#endif
 #if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
     && !defined(TARGET_S390X)
     qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
qmp.c +0 -7
index 7b6861846f..afa266ec1e 100644
@@ -546,13 +546,6 @@ CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
     return arch_query_cpu_definitions(errp);
 }
 
-CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
-                                                     CpuModelInfo *model,
-                                                     Error **errp)
-{
-    return arch_query_cpu_model_expansion(type, model, errp);
-}
-
 void qmp_add_client(const char *protocol, const char *fdname,
                     bool has_skipauth, bool skipauth, bool has_tls, bool tls,
                     Error **errp)
stubs/arch-query-cpu-model-expansion.c deleted +0 -12
deleted file mode 100644
index ae7cf554d1..0000000000
@@ -1,12 +0,0 @@
-#include "qemu/osdep.h"
-#include "qemu-common.h"
-#include "sysemu/arch_init.h"
-#include "qapi/qmp/qerror.h"
-
-CpuModelExpansionInfo *arch_query_cpu_model_expansion(CpuModelExpansionType type,
-                                                      CpuModelInfo *mode,
-                                                      Error **errp)
-{
-    error_setg(errp, QERR_UNSUPPORTED);
-    return NULL;
-}
target/i386/cpu.c +1 -1
index ddc45abd70..d683e70a13 100644
@@ -2574,7 +2574,7 @@ out:
 }
 
 CpuModelExpansionInfo *
-arch_query_cpu_model_expansion(CpuModelExpansionType type,
+qmp_query_cpu_model_expansion(CpuModelExpansionType type,
                                                       CpuModelInfo *model,
                                                       Error **errp)
 {
target/s390x/cpu_models.c +2 -1
index cdff9cdd3b..863dce064f 100644
@@ -22,6 +22,7 @@
 #ifndef CONFIG_USER_ONLY
 #include "sysemu/arch_init.h"
 #endif
+#include "qmp-commands.h"
 
 #define CPUDEF_INIT(_type, _gen, _ec_ga, _mha_pow, _hmfai, _name, _desc) \
     {                                                                    \
@@ -520,7 +521,7 @@ static void cpu_info_from_model(CpuModelInfo *info, const S390CPUModel *model,
     }
 }
 
-CpuModelExpansionInfo *arch_query_cpu_model_expansion(CpuModelExpansionType type,
+CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
                                                       CpuModelInfo *model,
                                                       Error **errp)
 {
stubs/Makefile.objs +0 -1
index 049d389966..dcfe6f49f9 100644
@@ -1,5 +1,4 @@
 stub-obj-y += arch-query-cpu-def.o
-stub-obj-y += arch-query-cpu-model-expansion.o
 stub-obj-y += bdrv-next-monitor-owned.o
 stub-obj-y += blk-commit-all.o
 stub-obj-y += blockdev-close-all-bdrv-states.o
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#63 lore raw

PATCH 26/26 qapi: make query-cpu-definitions depend on specific targets

In reply to Marc-André Lureau

It depends on TARGET_PPC || TARGET_ARM || TARGET_I386 || TARGET_S390X.

---
 qapi-schema.json            |  4 +++-
 include/sysemu/arch_init.h  |  2 --
 monitor.c                   | 22 ----------------------
 qmp.c                       |  5 -----
 stubs/arch-query-cpu-def.c  | 10 ----------
 target/arm/helper.c         |  3 ++-
 target/i386/cpu.c           |  3 ++-
 target/ppc/translate_init.c |  3 ++-
 target/s390x/cpu_models.c   |  2 +-
 stubs/Makefile.objs         |  1 -
 10 files changed, 10 insertions(+), 45 deletions(-)
 delete mode 100644 stubs/arch-query-cpu-def.c
qapi-schema.json +3 -1
index f5e1acff83..8e3949bca8 100644
@@ -4433,7 +4433,9 @@
 #
 # Since: 1.2.0
 ##
-{ 'command': 'query-cpu-definitions', 'returns': ['CpuDefinitionInfo'] }
+{ 'command': 'query-cpu-definitions', 'returns': ['CpuDefinitionInfo'],
+  'if': ['defined(NEED_CPU_H)',
+         'defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_I386) || defined(TARGET_S390X)'] }
 
 ##
 # @CpuModelInfo:
include/sysemu/arch_init.h +0 -2
index fb3d20a1b8..e9721b9ce8 100644
@@ -31,6 +31,4 @@ extern const uint32_t arch_type;
 int kvm_available(void);
 int xen_available(void);
 
-CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp);
-
 #endif
monitor.c +0 -22
index b134c39144..6600819599 100644
@@ -956,26 +956,6 @@ static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
     *ret_data = qobject_from_qlit(&qmp_schema_qlit);
 }
 
-/*
- * We used to define commands in qmp-commands.hx in addition to the
- * QAPI schema.  This permitted defining some of them only in certain
- * configurations.  query-commands has always reflected that (good,
- * because it lets QMP clients figure out what's actually available),
- * while query-qmp-schema never did (not so good).  This function is a
- * hack to keep the configuration-specific commands defined exactly as
- * before, even though qmp-commands.hx is gone.
- *
- * FIXME Educate the QAPI schema on configuration-specific commands,
- * and drop this hack.
- */
-static void qmp_unregister_commands_hack(void)
-{
-#if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
-    && !defined(TARGET_S390X)
-    qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
-#endif
-}
-
 void monitor_init_qmp_commands(void)
 {
     /*
@@ -995,8 +975,6 @@ void monitor_init_qmp_commands(void)
     qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
                          QCO_NO_OPTIONS);
 
-    qmp_unregister_commands_hack();
-
     QTAILQ_INIT(&qmp_cap_negotiation_commands);
     qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
                          qmp_marshal_qmp_capabilities, QCO_NO_OPTIONS);
qmp.c +0 -5
index afa266ec1e..d57ccf1251 100644
@@ -541,11 +541,6 @@ DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
     return prop_list;
 }
 
-CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
-{
-    return arch_query_cpu_definitions(errp);
-}
-
 void qmp_add_client(const char *protocol, const char *fdname,
                     bool has_skipauth, bool skipauth, bool has_tls, bool tls,
                     Error **errp)
stubs/arch-query-cpu-def.c deleted +0 -10
deleted file mode 100644
index cefe4beb82..0000000000
@@ -1,10 +0,0 @@
-#include "qemu/osdep.h"
-#include "qemu-common.h"
-#include "sysemu/arch_init.h"
-#include "qapi/qmp/qerror.h"
-
-CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
-{
-    error_setg(errp, QERR_UNSUPPORTED);
-    return NULL;
-}
target/arm/helper.c +2 -1
index 4ed32c56b8..ec644f3930 100644
@@ -15,6 +15,7 @@
 #include <zlib.h> /* For crc32 */
 #include "exec/semihost.h"
 #include "sysemu/kvm.h"
+#include "qmp-commands.h"
 
 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
 
@@ -5336,7 +5337,7 @@ static void arm_cpu_add_definition(gpointer data, gpointer user_data)
     *cpu_list = entry;
 }
 
-CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
+CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
 {
     CpuDefinitionInfoList *cpu_list = NULL;
     GSList *list;
target/i386/cpu.c +2 -1
index d683e70a13..e5f61f6bff 100644
@@ -36,6 +36,7 @@
 #include "qapi/visitor.h"
 #include "qom/qom-qobject.h"
 #include "sysemu/arch_init.h"
+#include "qmp-commands.h"
 
 #if defined(CONFIG_KVM)
 #include <linux/kvm_para.h>
@@ -2318,7 +2319,7 @@ static void x86_cpu_definition_entry(gpointer data, gpointer user_data)
     *cpu_list = entry;
 }
 
-CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
+CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
 {
     CpuDefinitionInfoList *cpu_list = NULL;
     GSList *list = get_sorted_cpu_model_list();
target/ppc/translate_init.c +2 -1
index 01723bdfec..2a2d62e5bb 100644
@@ -34,6 +34,7 @@
 #include "hw/ppc/ppc.h"
 #include "mmu-book3s-v3.h"
 #include "sysemu/qtest.h"
+#include "qmp-commands.h"
 
 //#define PPC_DUMP_CPU
 //#define PPC_DEBUG_SPR
@@ -10391,7 +10392,7 @@ static void ppc_cpu_defs_entry(gpointer data, gpointer user_data)
     *first = entry;
 }
 
-CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
+CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
 {
     CpuDefinitionInfoList *cpu_list = NULL;
     GSList *list;
target/s390x/cpu_models.c +1 -1
index 863dce064f..8021dda341 100644
@@ -387,7 +387,7 @@ static void create_cpu_model_list(ObjectClass *klass, void *opaque)
     *cpu_list = entry;
 }
 
-CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
+CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
 {
     struct CpuDefinitionInfoListData list_data = {
         .list = NULL,
stubs/Makefile.objs +0 -1
index dcfe6f49f9..71af433f6b 100644
@@ -1,4 +1,3 @@
-stub-obj-y += arch-query-cpu-def.o
 stub-obj-y += bdrv-next-monitor-owned.o
 stub-obj-y += blk-commit-all.o
 stub-obj-y += blockdev-close-all-bdrv-states.o
Signature · 1 line
2.14.0.rc0.1.g40ca67566
#64 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 26/26] qapi: make query-cpu-definitions depend on specific targets

Quoted 61 lines · Marc-André Lureau

It depends on TARGET_PPC || TARGET_ARM || TARGET_I386 || TARGET_S390X.

---
 qapi-schema.json            |  4 +++-
 include/sysemu/arch_init.h  |  2 --
 monitor.c                   | 22 ----------------------
 qmp.c                       |  5 -----
 stubs/arch-query-cpu-def.c  | 10 ----------
 target/arm/helper.c         |  3 ++-
 target/i386/cpu.c           |  3 ++-
 target/ppc/translate_init.c |  3 ++-
 target/s390x/cpu_models.c   |  2 +-
 stubs/Makefile.objs         |  1 -
 10 files changed, 10 insertions(+), 45 deletions(-)
 delete mode 100644 stubs/arch-query-cpu-def.c
qapi-schema.json +3 -1
index f5e1acff83..8e3949bca8 100644
@@ -4433,7 +4433,9 @@
 #
 # Since: 1.2.0
 ##
-{ 'command': 'query-cpu-definitions', 'returns': ['CpuDefinitionInfo'] }
+{ 'command': 'query-cpu-definitions', 'returns': ['CpuDefinitionInfo'],
+  'if': ['defined(NEED_CPU_H)',
+         'defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_I386) || defined(TARGET_S390X)'] }
 
 ##
 # @CpuModelInfo:
include/sysemu/arch_init.h +0 -2
index fb3d20a1b8..e9721b9ce8 100644
@@ -31,6 +31,4 @@ extern const uint32_t arch_type;
 int kvm_available(void);
 int xen_available(void);
 
-CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp);
-
 #endif
monitor.c +0 -10
index b134c39144..6600819599 100644
@@ -956,26 +956,6 @@ static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
     *ret_data = qobject_from_qlit(&qmp_schema_qlit);
 }
 
-/*
- * We used to define commands in qmp-commands.hx in addition to the
- * QAPI schema.  This permitted defining some of them only in certain
- * configurations.  query-commands has always reflected that (good,
- * because it lets QMP clients figure out what's actually available),
- * while query-qmp-schema never did (not so good).  This function is a
- * hack to keep the configuration-specific commands defined exactly as
- * before, even though qmp-commands.hx is gone.
- *
- * FIXME Educate the QAPI schema on configuration-specific commands,

monitor.c static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data, · line 978

Quoted 10 lines
monitor.c +0 -10
- * and drop this hack.
- */
-static void qmp_unregister_commands_hack(void)
-{
-#if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
-    && !defined(TARGET_S390X)
-    qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
-#endif
-}
-

Very nice to see this gone. Its removal could be made a separate commit to highlight the achievement :)

There are a few more candidates:

* QERR_FEATURE_DISABLED leads me to
- query-hotpluggable-cpus via monitor.c
- x-colo-lost-heartbeat via colo-failover.c
- query-rocker, query-rocker-ports, query-rocker-of-dpa-flows, query-rocker-of-dpa-groups via qmp-norocker.c

* QERR_UNSUPPORTED leads me to
- dump-guest-memory via dump_init() and stubs/dump.c
- query-vm-generation-id via stubs/vmgenid.c
- inject-nmi via nmi_monitor_handle() and s390_nmi()
- query-pci via pci-stub.c

* grep error_set stubs/* doesn't find more

Quoted 129 lines
monitor.c +0 -2
 void monitor_init_qmp_commands(void)
 {
     /*
@@ -995,8 +975,6 @@ void monitor_init_qmp_commands(void)
     qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
                          QCO_NO_OPTIONS);
 
-    qmp_unregister_commands_hack();
-
     QTAILQ_INIT(&qmp_cap_negotiation_commands);
     qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
                          qmp_marshal_qmp_capabilities, QCO_NO_OPTIONS);
qmp.c +0 -5
index afa266ec1e..d57ccf1251 100644
@@ -541,11 +541,6 @@ DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
     return prop_list;
 }
 
-CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
-{
-    return arch_query_cpu_definitions(errp);
-}
-
 void qmp_add_client(const char *protocol, const char *fdname,
                     bool has_skipauth, bool skipauth, bool has_tls, bool tls,
                     Error **errp)
stubs/arch-query-cpu-def.c deleted +0 -10
deleted file mode 100644
index cefe4beb82..0000000000
@@ -1,10 +0,0 @@
-#include "qemu/osdep.h"
-#include "qemu-common.h"
-#include "sysemu/arch_init.h"
-#include "qapi/qmp/qerror.h"
-
-CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
-{
-    error_setg(errp, QERR_UNSUPPORTED);
-    return NULL;
-}
target/arm/helper.c +2 -1
index 4ed32c56b8..ec644f3930 100644
@@ -15,6 +15,7 @@
 #include <zlib.h> /* For crc32 */
 #include "exec/semihost.h"
 #include "sysemu/kvm.h"
+#include "qmp-commands.h"
 
 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
 
@@ -5336,7 +5337,7 @@ static void arm_cpu_add_definition(gpointer data, gpointer user_data)
     *cpu_list = entry;
 }
 
-CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
+CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
 {
     CpuDefinitionInfoList *cpu_list = NULL;
     GSList *list;
target/i386/cpu.c +2 -1
index d683e70a13..e5f61f6bff 100644
@@ -36,6 +36,7 @@
 #include "qapi/visitor.h"
 #include "qom/qom-qobject.h"
 #include "sysemu/arch_init.h"
+#include "qmp-commands.h"
 
 #if defined(CONFIG_KVM)
 #include <linux/kvm_para.h>
@@ -2318,7 +2319,7 @@ static void x86_cpu_definition_entry(gpointer data, gpointer user_data)
     *cpu_list = entry;
 }
 
-CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
+CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
 {
     CpuDefinitionInfoList *cpu_list = NULL;
     GSList *list = get_sorted_cpu_model_list();
target/ppc/translate_init.c +2 -1
index 01723bdfec..2a2d62e5bb 100644
@@ -34,6 +34,7 @@
 #include "hw/ppc/ppc.h"
 #include "mmu-book3s-v3.h"
 #include "sysemu/qtest.h"
+#include "qmp-commands.h"
 
 //#define PPC_DUMP_CPU
 //#define PPC_DEBUG_SPR
@@ -10391,7 +10392,7 @@ static void ppc_cpu_defs_entry(gpointer data, gpointer user_data)
     *first = entry;
 }
 
-CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
+CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
 {
     CpuDefinitionInfoList *cpu_list = NULL;
     GSList *list;
target/s390x/cpu_models.c +1 -1
index 863dce064f..8021dda341 100644
@@ -387,7 +387,7 @@ static void create_cpu_model_list(ObjectClass *klass, void *opaque)
     *cpu_list = entry;
 }
 
-CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
+CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
 {
     struct CpuDefinitionInfoListData list_data = {
         .list = NULL,
stubs/Makefile.objs +0 -1
index dcfe6f49f9..71af433f6b 100644
@@ -1,4 +1,3 @@
-stub-obj-y += arch-query-cpu-def.o
 stub-obj-y += bdrv-next-monitor-owned.o
 stub-obj-y += blk-commit-all.o
 stub-obj-y += blockdev-close-all-bdrv-states.o
#65 lore raw

Re: [Qemu-devel] [PATCH 26/26] qapi: make query-cpu-definitions depend on specific targets

Hi

Quoted 88 lines · Markus Armbruster
Quoted 22 lines · Marc-André Lureau

It depends on TARGET_PPC || TARGET_ARM || TARGET_I386 || TARGET_S390X.

---
 qapi-schema.json            |  4 +++-
 include/sysemu/arch_init.h  |  2 --
 monitor.c                   | 22 ----------------------
 qmp.c                       |  5 -----
 stubs/arch-query-cpu-def.c  | 10 ----------
 target/arm/helper.c         |  3 ++-
 target/i386/cpu.c           |  3 ++-
 target/ppc/translate_init.c |  3 ++-
 target/s390x/cpu_models.c   |  2 +-
 stubs/Makefile.objs         |  1 -
 10 files changed, 10 insertions(+), 45 deletions(-)
 delete mode 100644 stubs/arch-query-cpu-def.c
qapi-schema.json +0 -0
index f5e1acff83..8e3949bca8 100644

qapi-schema.json line 4438

Quoted 7 lines
qapi-schema.json +3 -1
@@ -4433,7 +4433,9 @@
 #
 # Since: 1.2.0
 ##
-{ 'command': 'query-cpu-definitions', 'returns': ['CpuDefinitionInfo'] }
+{ 'command': 'query-cpu-definitions', 'returns': ['CpuDefinitionInfo'],
+  'if': ['defined(NEED_CPU_H)',
+         'defined(TARGET_PPC) || defined(TARGET_ARM) ||

defined(TARGET_I386) || defined(TARGET_S390X)'] }

monitor.c static void qmp_query_qmp_schema(QDict *qdict,

Quoted 19 lines
qapi-schema.json +0 -0
 ##
 # @CpuModelInfo:
include/sysemu/arch_init.h +0 -2
index fb3d20a1b8..e9721b9ce8 100644
@@ -31,6 +31,4 @@ extern const uint32_t arch_type;
 int kvm_available(void);
 int xen_available(void);
 
-CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp);
-
 #endif
monitor.c +0 -0
index b134c39144..6600819599 100644
@@ -956,26 +956,6 @@ static void qmp_query_qmp_schema(QDict *qdict,

QObject **ret_data,

Quoted 8 lines
monitor.c +0 -5
     *ret_data = qobject_from_qlit(&qmp_schema_qlit);
 }
 
-/*
- * We used to define commands in qmp-commands.hx in addition to the
- * QAPI schema.  This permitted defining some of them only in certain
- * configurations.  query-commands has always reflected that (good,
- * because it lets QMP clients figure out what's actually available),

monitor.c static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,

Quoted 10 lines
monitor.c +0 -10
- * while query-qmp-schema never did (not so good).  This function is a
- * hack to keep the configuration-specific commands defined exactly as
- * before, even though qmp-commands.hx is gone.
- *
- * FIXME Educate the QAPI schema on configuration-specific commands,
- * and drop this hack.
- */
-static void qmp_unregister_commands_hack(void)
-{
-#if !defined(TARGET_PPC) && !defined(TARGET_ARM) &&

!defined(TARGET_I386) \

monitor.c static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data, · line 978

Quoted 5 lines
monitor.c +0 -5
-    && !defined(TARGET_S390X)
-    qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
-#endif
-}
-

Very nice to see this gone. Its removal could be made a separate commit to highlight the achievement :)

There are a few more candidates:

* QERR_FEATURE_DISABLED leads me to
- query-hotpluggable-cpus via monitor.c
- x-colo-lost-heartbeat via colo-failover.c
- query-rocker, query-rocker-ports, query-rocker-of-dpa-flows, query-rocker-of-dpa-groups via qmp-norocker.c

Quoted 8 lines

* QERR_UNSUPPORTED leads me to
- dump-guest-memory via dump_init() and stubs/dump.c
- query-vm-generation-id via stubs/vmgenid.c
- inject-nmi via nmi_monitor_handle() and s390_nmi()
- query-pci via pci-stub.c

* grep error_set stubs/* doesn't find more

I have started to look at other opportunities in
https://github.com/elmarco/qemu/commits/qapi-if-more, but I have to revisit that once we have that series ready. I guess this could be done case by case later, and collaboratively.

Thanks for the review so far, I'll get back to it soon.

Quoted 123 lines

monitor.c void monitor_init_qmp_commands(void) · line 979

Quoted 11 lines
monitor.c +0 -2
 void monitor_init_qmp_commands(void)
 {
     /*
@@ -995,8 +975,6 @@ void monitor_init_qmp_commands(void)
     qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
                          QCO_NO_OPTIONS);
 
-    qmp_unregister_commands_hack();
-
     QTAILQ_INIT(&qmp_cap_negotiation_commands);
     qmp_register_command(&qmp_cap_negotiation_commands,

"qmp_capabilities",

qmp.c DevicePropertyInfoList

Quoted 6 lines
monitor.c +0 -0
                          qmp_marshal_qmp_capabilities, QCO_NO_OPTIONS);
qmp.c +0 -0
index afa266ec1e..d57ccf1251 100644
@@ -541,11 +541,6 @@ DevicePropertyInfoList

*qmp_device_list_properties(const char *typename,

qmp.c DevicePropertyInfoList *qmp_device_list_properties(const char *typename,

Quoted 10 lines
qmp.c +0 -5
     return prop_list;
 }
 
-CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
-{
-    return arch_query_cpu_definitions(errp);
-}
-
 void qmp_add_client(const char *protocol, const char *fdname,
                     bool has_skipauth, bool skipauth, bool has_tls,

bool tls,

Quoted 22 lines
qmp.c +0 -0
                     Error **errp)
stubs/arch-query-cpu-def.c deleted +0 -10
deleted file mode 100644
index cefe4beb82..0000000000
@@ -1,10 +0,0 @@
-#include "qemu/osdep.h"
-#include "qemu-common.h"
-#include "sysemu/arch_init.h"
-#include "qapi/qmp/qerror.h"
-
-CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
-{
-    error_setg(errp, QERR_UNSUPPORTED);
-    return NULL;
-}
target/arm/helper.c +0 -0
index 4ed32c56b8..ec644f3930 100644

target/arm/helper.c line 20

Quoted 6 lines
target/arm/helper.c +1 -0
@@ -15,6 +15,7 @@
 #include <zlib.h> /* For crc32 */
 #include "exec/semihost.h"
 #include "sysemu/kvm.h"
+#include "qmp-commands.h"
 
 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable

*/

target/arm/helper.c static void arm_cpu_add_definition(gpointer data,

Quoted 2 lines
target/arm/helper.c +0 -0
@@ -5336,7 +5337,7 @@ static void arm_cpu_add_definition(gpointer data,

gpointer user_data)

target/i386/cpu.c static void x86_cpu_definition_entry(gpointer

Quoted 21 lines
target/arm/helper.c +1 -1
     *cpu_list = entry;
 }
 
-CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
+CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
 {
     CpuDefinitionInfoList *cpu_list = NULL;
     GSList *list;
target/i386/cpu.c +1 -0
index d683e70a13..e5f61f6bff 100644
@@ -36,6 +36,7 @@
 #include "qapi/visitor.h"
 #include "qom/qom-qobject.h"
 #include "sysemu/arch_init.h"
+#include "qmp-commands.h"
 
 #if defined(CONFIG_KVM)
 #include <linux/kvm_para.h>
@@ -2318,7 +2319,7 @@ static void x86_cpu_definition_entry(gpointer

data, gpointer user_data)

target/ppc/translate_init.c static void ppc_cpu_defs_entry(gpointer data,

Quoted 21 lines
target/i386/cpu.c +1 -1
     *cpu_list = entry;
 }
 
-CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
+CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
 {
     CpuDefinitionInfoList *cpu_list = NULL;
     GSList *list = get_sorted_cpu_model_list();
target/ppc/translate_init.c +1 -0
index 01723bdfec..2a2d62e5bb 100644
@@ -34,6 +34,7 @@
 #include "hw/ppc/ppc.h"
 #include "mmu-book3s-v3.h"
 #include "sysemu/qtest.h"
+#include "qmp-commands.h"
 
 //#define PPC_DUMP_CPU
 //#define PPC_DEBUG_SPR
@@ -10391,7 +10392,7 @@ static void ppc_cpu_defs_entry(gpointer data,

gpointer user_data)

target/s390x/cpu_models.c static void create_cpu_model_list(ObjectClass

Quoted 13 lines
target/ppc/translate_init.c +1 -1
     *first = entry;
 }
 
-CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
+CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
 {
     CpuDefinitionInfoList *cpu_list = NULL;
     GSList *list;
target/s390x/cpu_models.c +0 -0
index 863dce064f..8021dda341 100644
@@ -387,7 +387,7 @@ static void create_cpu_model_list(ObjectClass

*klass, void *opaque)

Quoted 3 lines
target/s390x/cpu_models.c +0 -0
     *cpu_list = entry;
 }
 
Quoted 16 lines
Quoted 10 lines
target/s390x/cpu_models.c +1 -1
-CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
+CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
 {
     struct CpuDefinitionInfoListData list_data = {
         .list = NULL,
stubs/Makefile.objs +0 -0
index dcfe6f49f9..71af433f6b 100644

stubs/Makefile.objs line 3

Quoted 4 lines
stubs/Makefile.objs +0 -1
@@ -1,4 +1,3 @@
-stub-obj-y += arch-query-cpu-def.o
 stub-obj-y += bdrv-next-monitor-owned.o
 stub-obj-y += blk-commit-all.o
 stub-obj-y += blockdev-close-all-bdrv-states.o

--

Marc-André Lureau

#66 lore raw
In reply to Marc-André Lureau
Quoted 29 lines · Marc-André Lureau

Hi,

In order to clean-up some hacks in qapi (having to unregister commands at runtime), I proposed a "[PATCH v5 02/20] qapi.py: add a simple #ifdef condition"

(see http://lists.gnu.org/archive/html/qemu-devel/2016-08/msg03106.html).

However, we decided to drop that patch from the series and solve the problem later. The main issues were:
- the syntax was awkward to the JSON schema and documentation
- the evaluation of the condition was done in the qapi scripts, with very limited capability
- each target/config would need different generated files.

Instead, it could defer the #if evaluation to the C-preprocessor.

With this series, top-level qapi JSON entity can take 'if' keys:

{ 'struct': 'TestIfStruct', 'data': { 'foo': 'int' },
'if': 'defined(TEST_IF_STRUCT)' }

Members can be exploded as dictionnary with 'type'/'if' keys:

{ 'struct': 'TestIfStruct', 'data':
{ 'foo': 'int',

    'bar': { 'type': 'int', 'if': 'defined(TEST_IF_STRUCT_BAR)'} } }

Enum values can be exploded as dictionnary with 'type'/'if' keys:

Quoted 8 lines

{ 'enum': 'TestIfEnum', 'data':
[ 'foo',

    { 'name' : 'bar', 'if': 'defined(TEST_IF_ENUM_BAR)' } ] }

A good benefit from having conditional schema is that introspection will reflect more accurately the capability of the server. Another benefit is that it may help to remove some dead code when disabling a functionality.

This is the main benefit. Until we realize it, introspection remains seriously hobbled.

A few closing remarks.

The general approach "generate the #if for the compiler to evaluate" looks sound.

I haven't been able to fully review how it's integrated into the QAPI language and how the generators implement it. I hope a bit of judicious patch splitting will help me over the hump.

As so often, solving one problem makes other problems more visible. In this case, the problem that we generate a monolith, and pay for that with compile time. More of it once we compile the monolith per target.

Quoted 6 lines

Starting from patch "qapi: add conditions to VNC type/commands/events on the schema", the series demonstrate adding conditions, in order to remove qmp_unregister_commands_hack(). However, it feels like I cheated a little bit by using per-target NEED_CPU_H in the headers to avoid #define poison. The alternative could be to split the headers in common/target?

Yup, we could really use a way to modularize the generated code.

If your work leads us to ideas on how to crack the monolith, great. If not, we'll have to decide whether we can live with the compile time hit. I'd rather not block your work on cracking the monolith.

Quoted 5 lines

There are a lot more things we could make conditional in the QAPI schema, like pci/kvm/xen/numa/vde/slirp/posix/win32/vsock/lzo etc etc, however I am still evaluating the implication of such changes both externally and internally, for those interested, I can share my wip branch.

You provide the infrastructure and useful examples of use. Good enough, no need to hunt down *all* uses right away.

#67 lore raw

Hi

Quoted 43 lines · Markus Armbruster
Quoted 29 lines · Marc-André Lureau

Hi,

In order to clean-up some hacks in qapi (having to unregister commands at runtime), I proposed a "[PATCH v5 02/20] qapi.py: add a simple #ifdef condition"

(see http://lists.gnu.org/archive/html/qemu-devel/2016-08/msg03106.html).

However, we decided to drop that patch from the series and solve the problem later. The main issues were:
- the syntax was awkward to the JSON schema and documentation
- the evaluation of the condition was done in the qapi scripts, with very limited capability
- each target/config would need different generated files.

Instead, it could defer the #if evaluation to the C-preprocessor.

With this series, top-level qapi JSON entity can take 'if' keys:

{ 'struct': 'TestIfStruct', 'data': { 'foo': 'int' },
'if': 'defined(TEST_IF_STRUCT)' }

Members can be exploded as dictionnary with 'type'/'if' keys:

{ 'struct': 'TestIfStruct', 'data':
{ 'foo': 'int',

    'bar': { 'type': 'int', 'if': 'defined(TEST_IF_STRUCT_BAR)'} } }

Enum values can be exploded as dictionnary with 'type'/'if' keys:

Quoted 8 lines

{ 'enum': 'TestIfEnum', 'data':
[ 'foo',

    { 'name' : 'bar', 'if': 'defined(TEST_IF_ENUM_BAR)' } ] }

A good benefit from having conditional schema is that introspection will reflect more accurately the capability of the server. Another benefit is that it may help to remove some dead code when disabling a functionality.

This is the main benefit. Until we realize it, introspection remains seriously hobbled.

Quoted 8 lines

A few closing remarks.

The general approach "generate the #if for the compiler to evaluate" looks sound.

I haven't been able to fully review how it's integrated into the QAPI language and how the generators implement it. I hope a bit of judicious patch splitting will help me over the hump.

I have done some patch splitting, that doubles the number of patches though ;)

Quoted 4 lines

As so often, solving one problem makes other problems more visible. In this case, the problem that we generate a monolith, and pay for that with compile time. More of it once we compile the monolith per target.

Indeed, it would be nice to improve that soon after.

Quoted 8 lines
Quoted 7 lines

Starting from patch "qapi: add conditions to VNC type/commands/events on the schema", the series demonstrate adding conditions, in order to remove qmp_unregister_commands_hack(). However, it feels like I cheated a little bit by using per-target NEED_CPU_H in the headers to avoid #define poison. The alternative could be to split the headers in common/target?

Quoted 5 lines

Yup, we could really use a way to modularize the generated code.

If your work leads us to ideas on how to crack the monolith, great. If not, we'll have to decide whether we can live with the compile time hit. I'd rather not block your work on cracking the monolith.

I think we could start by splitting the json schemas.

Quoted 10 lines
Quoted 6 lines

There are a lot more things we could make conditional in the QAPI schema, like pci/kvm/xen/numa/vde/slirp/posix/win32/vsock/lzo etc etc, however I am still evaluating the implication of such changes both externally and internally, for those interested, I can share my wip branch.

You provide the infrastructure and useful examples of use. Good enough, no need to hunt down *all* uses right away.

I am sending a new version,

thanks

Signature · 1 line
Marc-André Lureau
#68 lore raw
Quoted 48 lines · Marc-André Lureau

Hi

Quoted 45 lines · Markus Armbruster
Quoted 29 lines · Marc-André Lureau

Hi,

In order to clean-up some hacks in qapi (having to unregister commands at runtime), I proposed a "[PATCH v5 02/20] qapi.py: add a simple #ifdef condition"

(see http://lists.gnu.org/archive/html/qemu-devel/2016-08/msg03106.html).

However, we decided to drop that patch from the series and solve the problem later. The main issues were:
- the syntax was awkward to the JSON schema and documentation
- the evaluation of the condition was done in the qapi scripts, with very limited capability
- each target/config would need different generated files.

Instead, it could defer the #if evaluation to the C-preprocessor.

With this series, top-level qapi JSON entity can take 'if' keys:

{ 'struct': 'TestIfStruct', 'data': { 'foo': 'int' },
'if': 'defined(TEST_IF_STRUCT)' }

Members can be exploded as dictionnary with 'type'/'if' keys:

{ 'struct': 'TestIfStruct', 'data':
{ 'foo': 'int',

    'bar': { 'type': 'int', 'if': 'defined(TEST_IF_STRUCT_BAR)'} } }

Enum values can be exploded as dictionnary with 'type'/'if' keys:

Quoted 8 lines

{ 'enum': 'TestIfEnum', 'data':
[ 'foo',

    { 'name' : 'bar', 'if': 'defined(TEST_IF_ENUM_BAR)' } ] }

A good benefit from having conditional schema is that introspection will reflect more accurately the capability of the server. Another benefit is that it may help to remove some dead code when disabling a functionality.

This is the main benefit. Until we realize it, introspection remains seriously hobbled.

A few closing remarks.

Quoted 8 lines
Quoted 6 lines

The general approach "generate the #if for the compiler to evaluate" looks sound.

I haven't been able to fully review how it's integrated into the QAPI language and how the generators implement it. I hope a bit of judicious patch splitting will help me over the hump.

I have done some patch splitting, that doubles the number of patches though ;)

A big pile of patches can look scary, but what really drags out review is oversized non-trivial patches like PATCH 07. I can take quick, refreshing breaks much more easily between patches than within a big & hairy one.

Quoted 14 lines
Quoted 3 lines

As so often, solving one problem makes other problems more visible. In this case, the problem that we generate a monolith, and pay for that with compile time. More of it once we compile the monolith per target.

Indeed, it would be nice to improve that soon after.

Quoted 8 lines
Quoted 7 lines

Starting from patch "qapi: add conditions to VNC type/commands/events on the schema", the series demonstrate adding conditions, in order to remove qmp_unregister_commands_hack(). However, it feels like I cheated a little bit by using per-target NEED_CPU_H in the headers to avoid #define poison. The alternative could be to split the headers in common/target?

Quoted 7 lines
Quoted 5 lines

Yup, we could really use a way to modularize the generated code.

If your work leads us to ideas on how to crack the monolith, great. If not, we'll have to decide whether we can live with the compile time hit. I'd rather not block your work on cracking the monolith.

I think we could start by splitting the json schemas.

The way things work, QMP needs to be defined in a single schema. Doesn't mean we have to generate monoliths from it.

Quoted 13 lines
Quoted 9 lines
Quoted 5 lines

There are a lot more things we could make conditional in the QAPI schema, like pci/kvm/xen/numa/vde/slirp/posix/win32/vsock/lzo etc etc, however I am still evaluating the implication of such changes both externally and internally, for those interested, I can share my wip branch.

You provide the infrastructure and useful examples of use. Good enough, no need to hunt down *all* uses right away.

I am sending a new version,

thanks