Hi,
v7 cover Hi,
This is the second part of the "add #if pre-processor conditions to generated code" series, adding schema member conditions (roughly 16-38/49).
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)' } ] }v7:
- replaced osdep.h include by a #warning check
- renamed event_names/event_enum_members, enum_name/event_enum_ename
- move normalization after checking
- simplify make_enum_members()
- some tests changes
- rebase/conflicts tweaks
- squash documentation changes in related patches
- tweak generated documentation code & output
- modify commit comments, add rb tags
v6:
- subset of v5 series: add schema member conditions
- split "qapi: factor out check_known_keys()", error report improvements
- add a patch to "qapi: include osdep.h in type headers" to avoid potential build configuration change issues
- rebased, on top of Markus's qapi-next branch (first 4 patches, included for patchew testing)
Marc-André Lureau (27):
qapi: make sure osdep.h is included in type headers
qapi: do not define enumeration value explicitly
qapi: rename QAPISchemaEnumType.values to .members
qapi: change enum visitor and gen_enum* to take QAPISchemaMember
tests: print enum type members more like object type members
qapi: factor out checking for keys
qapi: improve reporting of unknown or missing keys
qapi: add a dictionary form with 'name' key for enum members
qapi: add 'if' to enum members
qapi-events: add 'if' condition to implicit event enum
qapi: pass long form enum to make_enum_members
qapi: rename allow_dict to allow_implicit
qapi: add a dictionary form for TYPE
qapi: add 'if' to implicit struct members
qapi: add an error in case a discriminator is conditional
qapi: add 'if' to union members
qapi: simplify make_enum_members()
tests/qapi: add command with condition on union argument
qapi: add 'if' to alternate members
tests/qapi: add command with condition on alternate argument
qapi: add #if conditions to generated code members
qapi: add 'If:' condition to enum values documentation
qapi: add 'If:' condition to struct members documentation
qapi: add condition to variants documentation
qapi: break long lines at 'data' member
qapi: add more conditions to SPICE
qapi: add conditions to REPLICATION type/commands on the schema
qapi/block-core.json | 26 +-
qapi/char.json | 150 ++++++-----
qapi/migration.json | 15 +-
qapi/misc.json | 7 +-
qapi/net.json | 3 +-
qapi/tpm.json | 5 +-
qapi/ui.json | 3 +-
scripts/qapi/common.py | 253 ++++++++++++------
scripts/qapi/doc.py | 30 ++-
scripts/qapi/events.py | 13 +-
scripts/qapi/introspect.py | 16 +-
scripts/qapi/types.py | 13 +-
scripts/qapi/visit.py | 8 +-
migration/colo.c | 16 +-
monitor.c | 5 -
docs/devel/qapi-code-gen.txt | 19 ++
tests/Makefile.include | 11 +-
tests/qapi-schema/alternate-base.err | 1 +
tests/qapi-schema/alternate-invalid-dict.err | 1 +
...ember.exit => alternate-invalid-dict.exit} | 0
tests/qapi-schema/alternate-invalid-dict.json | 4 +
...-member.out => alternate-invalid-dict.out} | 0
tests/qapi-schema/comments.out | 14 +-
tests/qapi-schema/doc-bad-section.out | 13 +-
tests/qapi-schema/doc-good.json | 11 +-
tests/qapi-schema/doc-good.out | 22 +-
tests/qapi-schema/doc-good.texi | 7 +-
tests/qapi-schema/double-type.err | 1 +
tests/qapi-schema/empty.out | 9 +-
tests/qapi-schema/enum-bad-member.err | 1 +
tests/qapi-schema/enum-bad-member.exit | 1 +
tests/qapi-schema/enum-bad-member.json | 2 +
tests/qapi-schema/enum-bad-member.out | 0
.../qapi-schema/enum-dict-member-unknown.err | 2 +
.../qapi-schema/enum-dict-member-unknown.exit | 1 +
.../qapi-schema/enum-dict-member-unknown.json | 2 +
.../qapi-schema/enum-dict-member-unknown.out | 0
tests/qapi-schema/enum-dict-member.err | 1 -
tests/qapi-schema/enum-dict-member.json | 2 -
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/event-case.out | 9 +-
.../qapi-schema/event-member-invalid-dict.err | 1 +
.../event-member-invalid-dict.exit | 1 +
.../event-member-invalid-dict.json | 2 +
.../qapi-schema/event-member-invalid-dict.out | 0
tests/qapi-schema/event-nest-struct.json | 2 +-
.../flat-union-inline-invalid-dict.err | 1 +
.../flat-union-inline-invalid-dict.exit | 1 +
.../flat-union-inline-invalid-dict.json | 11 +
.../flat-union-inline-invalid-dict.out | 0
tests/qapi-schema/flat-union-inline.json | 2 +-
.../flat-union-invalid-if-discriminator.err | 1 +
.../flat-union-invalid-if-discriminator.exit | 1 +
.../flat-union-invalid-if-discriminator.json | 17 ++
.../flat-union-invalid-if-discriminator.out | 0
tests/qapi-schema/ident-with-escape.out | 9 +-
tests/qapi-schema/include-relpath.out | 14 +-
tests/qapi-schema/include-repetition.out | 14 +-
tests/qapi-schema/include-simple.out | 14 +-
tests/qapi-schema/indented-expr.out | 9 +-
.../nested-struct-data-invalid-dict.err | 1 +
.../nested-struct-data-invalid-dict.exit | 1 +
.../nested-struct-data-invalid-dict.json | 3 +
.../nested-struct-data-invalid-dict.out | 0
tests/qapi-schema/nested-struct-data.json | 2 +-
tests/qapi-schema/qapi-schema-test.json | 39 ++-
tests/qapi-schema/qapi-schema-test.out | 74 ++++-
.../struct-member-invalid-dict.err | 1 +
.../struct-member-invalid-dict.exit | 1 +
.../struct-member-invalid-dict.json | 3 +
.../struct-member-invalid-dict.out | 0
tests/qapi-schema/test-qapi.py | 9 +-
.../qapi-schema/union-branch-invalid-dict.err | 1 +
.../union-branch-invalid-dict.exit | 1 +
.../union-branch-invalid-dict.json | 4 +
.../qapi-schema/union-branch-invalid-dict.out | 0
tests/qapi-schema/unknown-expr-key.err | 3 +-
tests/qapi-schema/unknown-expr-key.json | 2 +-
81 files changed, 681 insertions(+), 266 deletions(-)
create mode 100644 tests/qapi-schema/alternate-invalid-dict.err
rename tests/qapi-schema/{enum-dict-member.exit => alternate-invalid-dict.exit} (100%)
create mode 100644 tests/qapi-schema/alternate-invalid-dict.json
rename tests/qapi-schema/{enum-dict-member.out => alternate-invalid-dict.out} (100%)
create mode 100644 tests/qapi-schema/enum-bad-member.err
create mode 100644 tests/qapi-schema/enum-bad-member.exit
create mode 100644 tests/qapi-schema/enum-bad-member.json
create mode 100644 tests/qapi-schema/enum-bad-member.out
create mode 100644 tests/qapi-schema/enum-dict-member-unknown.err
create mode 100644 tests/qapi-schema/enum-dict-member-unknown.exit
create mode 100644 tests/qapi-schema/enum-dict-member-unknown.json
create mode 100644 tests/qapi-schema/enum-dict-member-unknown.out
delete mode 100644 tests/qapi-schema/enum-dict-member.err
delete mode 100644 tests/qapi-schema/enum-dict-member.json
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/event-member-invalid-dict.err
create mode 100644 tests/qapi-schema/event-member-invalid-dict.exit
create mode 100644 tests/qapi-schema/event-member-invalid-dict.json
create mode 100644 tests/qapi-schema/event-member-invalid-dict.out
create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.err
create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.exit
create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.json
create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.out
create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.err
create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.exit
create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.json
create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.out
create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.err
create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.exit
create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.json
create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.out
create mode 100644 tests/qapi-schema/struct-member-invalid-dict.err
create mode 100644 tests/qapi-schema/struct-member-invalid-dict.exit
create mode 100644 tests/qapi-schema/struct-member-invalid-dict.json
create mode 100644 tests/qapi-schema/struct-member-invalid-dict.out
create mode 100644 tests/qapi-schema/union-branch-invalid-dict.err
create mode 100644 tests/qapi-schema/union-branch-invalid-dict.exit
create mode 100644 tests/qapi-schema/union-branch-invalid-dict.json
create mode 100644 tests/qapi-schema/union-branch-invalid-dict.outSignature · 1 line
2.20.0.rc1
PATCH v7 1/27 qapi: make sure osdep.h is included in type headers
Now that the schema can be configured, it is crucial that all types are configured the same. Make sure config-host.h is included, by checking osdep.h inclusion. The build-sys tracks the dependency and rebuilds the types if the configuration changed.
--- scripts/qapi/types.py | 3 +++ 1 file changed, 3 insertions(+)
scripts/qapi/types.py +3 -0
@@ -201,6 +201,9 @@ class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): ''', types=types, visit=visit)) self._genh.preamble_add(mcgen(''' +#ifndef QEMU_OSDEP_H +#error Please include osdep.h first! +#endif #include "qapi/qapi-builtin-types.h" '''))
Signature · 1 line
2.20.0.rc1
Re: [Qemu-devel] [PATCH for-4.0 v7 01/27] qapi: make sure osdep.h is included in type headers
Quoted 15 lines · Marc-André Lureau
Now that the schema can be configured, it is crucial that all types are configured the same. Make sure
config-host.his included, by checkingosdep.hinclusion. The build-sys tracks the dependency and rebuilds the types if the configuration changed.Signed-off-by Marc-André Lureau <[email protected]>--- scripts/qapi/types.py | 3 +++ 1 file changed, 3 insertions(+)scripts/qapi/types.py +0
-0
scripts/qapi/types.py class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): · line 208
Quoted 8 lines
scripts/qapi/types.py +3
-0@@ -201,6 +201,9 @@ class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): ''', types=types, visit=visit)) self._genh.preamble_add(mcgen(''' +#ifndef QEMU_OSDEP_H +#error Please include osdep.h first! +#endif #include "qapi/qapi-builtin-types.h" '''))
I understand why you propose this patch. The QAPI-generated headers use #ifdef CONFIG_FOO. The configuration header "qemu/osdep.h" must be consistently included before the generated headers, or else you end up with nasty bugs, such as the same enum having different values in different .o, or the same struct having a different layout.
But this applies to *all* headers that use #ifdef. Why check it here, but not there? What makes the QAPI-generated headers special?
Re: [Qemu-devel] [PATCH for-4.0 v7 01/27] qapi: make sure osdep.h is included in type headers
Hi
Quoted 33 lines · Markus Armbruster
Quoted 15 lines · Marc-André Lureau
Now that the schema can be configured, it is crucial that all types are configured the same. Make sure
config-host.his included, by checkingosdep.hinclusion. The build-sys tracks the dependency and rebuilds the types if the configuration changed.Signed-off-by Marc-André Lureau <[email protected]>--- scripts/qapi/types.py | 3 +++ 1 file changed, 3 insertions(+)scripts/qapi/types.py +0
-0scripts/qapi/types.py class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): · line 208
Quoted 8 lines
scripts/qapi/types.py +3
-0@@ -201,6 +201,9 @@ class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): ''', types=types, visit=visit)) self._genh.preamble_add(mcgen(''' +#ifndef QEMU_OSDEP_H +#error Please include osdep.h first! +#endif #include "qapi/qapi-builtin-types.h" '''))I understand why you propose this patch. The QAPI-generated headers use #ifdef
CONFIG_FOO. The configuration header "qemu/osdep.h" must be consistently included before the generated headers, or else you end up with nasty bugs, such as the same enum having different values in different .o, or the same struct having a different layout.
Quoted 3 lines
But this applies to *all* headers that use #ifdef. Why check it here, but not there? What makes the QAPI-generated headers special?
It's the discussion about #if in headers (and enums in particular) that started this. We want to make sure all compilation units share the same data structure/ABI. I proposed to include osdep.h in qapi headers, but that was rejected.
The warning is a different approach. I agree it could apply to all headers. Do you think I should update all headers as well?
Signature · 1 line
Marc-André Lureau
Re: [Qemu-devel] [PATCH for-4.0 v7 01/27] qapi: make sure osdep.h is included in type headers
Quoted 40 lines · Marc-André Lureau
Hi
Quoted 33 lines · Markus Armbruster
Quoted 15 lines · Marc-André Lureau
Now that the schema can be configured, it is crucial that all types are configured the same. Make sure
config-host.his included, by checkingosdep.hinclusion. The build-sys tracks the dependency and rebuilds the types if the configuration changed.Signed-off-by Marc-André Lureau <[email protected]>--- scripts/qapi/types.py | 3 +++ 1 file changed, 3 insertions(+)scripts/qapi/types.py +0
-0scripts/qapi/types.py class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): · line 208
Quoted 8 lines
scripts/qapi/types.py +3
-0@@ -201,6 +201,9 @@ class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): ''', types=types, visit=visit)) self._genh.preamble_add(mcgen(''' +#ifndef QEMU_OSDEP_H +#error Please include osdep.h first! +#endif #include "qapi/qapi-builtin-types.h" '''))I understand why you propose this patch. The QAPI-generated headers use #ifdef
CONFIG_FOO. The configuration header "qemu/osdep.h" must be consistently included before the generated headers, or else you end up with nasty bugs, such as the same enum having different values in different .o, or the same struct having a different layout.Quoted 3 lines
But this applies to *all* headers that use #ifdef. Why check it here, but not there? What makes the QAPI-generated headers special?
Quoted 6 lines
It's the discussion about #if in headers (and enums in particular) that started this. We want to make sure all compilation units share the same data structure/ABI. I proposed to include
osdep.hin qapi headers, but that was rejected.
The warning is a different approach. I agree it could apply to all headers. Do you think I should update all headers as well?
That would replace the rule 'all .c files must include "qemu/osdep.h" first' by 'all .h files must check "qemu/osdep.h" has been included already', which is not an improvement.
All we have right now to help with enforcing our osdep.h rule is scripts/clean-includes. We run it periodically to fix rule breakers. It's manual, because we have a few .c files in the tree where the rule doesn't apply, and the script doesn't know about them. Fixable, I guess. Most recent run:
Subject: [PATCH] Clean up includes
Message-Id: <[email protected]>
https://lists.nongnu.org/archive/html/qemu-devel/2018-12/msg00605.htmlThe obvious improvement would be flagging rule breakers before they get committed.
checkpatch.pl could flag patches adding .c files that don't include "qemu/osdep.h" first. The "first" part might be a bit annoying to code. The "adding files" part already exists: checkpatch.pl warns when you add a file without updating MAINTAINERS.
checkpatch.pl could also flag patches removing #include "qemu/osdep.h".
Other projects use a syntax-check make target to check sources. If we decide we like that better than checkpatch.pl for some checks, we can do that.
Re: [Qemu-devel] [PATCH for-4.0 v7 01/27] qapi: make sure osdep.h is included in type headers
Hi
Quoted 50 lines · Markus Armbruster
Quoted 40 lines · Marc-André Lureau
Hi
Quoted 33 lines · Markus Armbruster
Quoted 15 lines · Marc-André Lureau
Now that the schema can be configured, it is crucial that all types are configured the same. Make sure
config-host.his included, by checkingosdep.hinclusion. The build-sys tracks the dependency and rebuilds the types if the configuration changed.Signed-off-by Marc-André Lureau <[email protected]>--- scripts/qapi/types.py | 3 +++ 1 file changed, 3 insertions(+)scripts/qapi/types.py +0
-0scripts/qapi/types.py class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): · line 208
Quoted 8 lines
scripts/qapi/types.py +3
-0@@ -201,6 +201,9 @@ class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): ''', types=types, visit=visit)) self._genh.preamble_add(mcgen(''' +#ifndef QEMU_OSDEP_H +#error Please include osdep.h first! +#endif #include "qapi/qapi-builtin-types.h" '''))I understand why you propose this patch. The QAPI-generated headers use #ifdef
CONFIG_FOO. The configuration header "qemu/osdep.h" must be consistently included before the generated headers, or else you end up with nasty bugs, such as the same enum having different values in different .o, or the same struct having a different layout.Quoted 3 lines
But this applies to *all* headers that use #ifdef. Why check it here, but not there? What makes the QAPI-generated headers special?
Quoted 6 lines
It's the discussion about #if in headers (and enums in particular) that started this. We want to make sure all compilation units share the same data structure/ABI. I proposed to include
osdep.hin qapi headers, but that was rejected.
The warning is a different approach. I agree it could apply to all headers. Do you think I should update all headers as well?
Quoted 3 lines
That would replace the rule 'all .c files must include "
qemu/osdep.h" first' by 'all .h files must check "qemu/osdep.h" has been included already', which is not an improvement.
That would be an improvement since headers may also be impacted by osdep.h
Alternatively, why not use -include ?
Quoted 24 lines
All we have right now to help with enforcing our
osdep.hrule isscripts/clean-includes. We run it periodically to fix rule breakers. It's manual, because we have a few .c files in the tree where the rule doesn't apply, and the script doesn't know about them. Fixable, I guess. Most recent run:Subject: [PATCH] Clean up includes Message-Id: <[email protected]> https://lists.nongnu.org/archive/html/qemu-devel/2018-12/msg00605.htmlThe obvious improvement would be flagging rule breakers before they get committed.
checkpatch.pl could flag patches adding .c files that don't include "
qemu/osdep.h" first. The "first" part might be a bit annoying to code. The "adding files" part already exists: checkpatch.pl warns when you add a file without updatingMAINTAINERS.checkpatch.pl could also flag patches removing #include "
qemu/osdep.h".Other projects use a syntax-check make target to check sources. If we decide we like that better than checkpatch.pl for some checks, we can do that.
Signature · 1 line
Marc-André Lureau
Re: [Qemu-devel] [PATCH for-4.0 v7 01/27] qapi: make sure osdep.h is included in type headers
Quoted 53 lines · Marc-André Lureau
Hi
Quoted 50 lines · Markus Armbruster
Quoted 46 lines · Marc-André Lureau
Hi
Quoted 33 lines · Markus Armbruster
Quoted 15 lines · Marc-André Lureau
Now that the schema can be configured, it is crucial that all types are configured the same. Make sure
config-host.his included, by checkingosdep.hinclusion. The build-sys tracks the dependency and rebuilds the types if the configuration changed.Signed-off-by Marc-André Lureau <[email protected]>--- scripts/qapi/types.py | 3 +++ 1 file changed, 3 insertions(+)scripts/qapi/types.py +0
-0scripts/qapi/types.py class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): · line 208
Quoted 8 lines
scripts/qapi/types.py +3
-0@@ -201,6 +201,9 @@ class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): ''', types=types, visit=visit)) self._genh.preamble_add(mcgen(''' +#ifndef QEMU_OSDEP_H +#error Please include osdep.h first! +#endif #include "qapi/qapi-builtin-types.h" '''))I understand why you propose this patch. The QAPI-generated headers use #ifdef
CONFIG_FOO. The configuration header "qemu/osdep.h" must be consistently included before the generated headers, or else you end up with nasty bugs, such as the same enum having different values in different .o, or the same struct having a different layout.Quoted 3 lines
But this applies to *all* headers that use #ifdef. Why check it here, but not there? What makes the QAPI-generated headers special?
It's the discussion about #if in headers (and enums in particular) that started this. We want to make sure all compilation units share the same data structure/ABI. I proposed to include
osdep.hin qapi headers, but that was rejected.
The warning is a different approach. I agree it could apply to all headers. Do you think I should update all headers as well?
Quoted 7 lines
Quoted 3 lines
That would replace the rule 'all .c files must include "
qemu/osdep.h" first' by 'all .h files must check "qemu/osdep.h" has been included already', which is not an improvement.That would be an improvement since headers may also be impacted by
osdep.hAlternatively, why not use -include ?
Requiring .c to include the configuration header first follows established Autoconf practice. The "first" part covers all headers.
I've seen plenty of autoconfiscated code, yet none where the headers double-check the configuration header has been included already. Such a check is neither sufficient nor really necessary. Checking the .c is both.
I have no strong feelings about using -include instead. I'd prefer to avoid the difference to what other projects do, though.
Circling back to the issue that made you propose this patch.
I think I'm (belatedly) coming around to your initial view that the use of conditionals in generated QAPI code is safe enough.
I shouldn't be worried about #if defined(CONFIG_HOST_THING) where CONFIG_HOST_THING belongs to config-host.h. That's always pulled in via qemu/osdep.h.
I also shouldn't be worried about #if defined(CONFIG_TARGET_THING) where CONFIG_TARGET_THING belongs to config-target.h. qemu/osdep.h pulls in either that or exec/poison.h.
I even shouldn't be worried about #if defined(RANDOM_MACRO) as long as qemu/osdep.h pulls in its owner or poisons it. I might legitimately be worried about the "as long as" part.
We could perhaps extract the ifconds and look for macros that belong neither to config-host.h nor config-target.h. But I'm not sure it's worth the bother.
Re: [Qemu-devel] [PATCH for-4.0 v7 01/27] qapi: make sure osdep.h is included in type headers
In reply to Markus ArmbrusterQuoted 63 lines · Markus Armbruster
Quoted 40 lines · Marc-André Lureau
Hi
Quoted 33 lines · Markus Armbruster
Quoted 15 lines · Marc-André Lureau
Now that the schema can be configured, it is crucial that all types are configured the same. Make sure
config-host.his included, by checkingosdep.hinclusion. The build-sys tracks the dependency and rebuilds the types if the configuration changed.Signed-off-by Marc-André Lureau <[email protected]>--- scripts/qapi/types.py | 3 +++ 1 file changed, 3 insertions(+)scripts/qapi/types.py +0
-0scripts/qapi/types.py class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): · line 208
Quoted 8 lines
scripts/qapi/types.py +3
-0@@ -201,6 +201,9 @@ class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): ''', types=types, visit=visit)) self._genh.preamble_add(mcgen(''' +#ifndef QEMU_OSDEP_H +#error Please include osdep.h first! +#endif #include "qapi/qapi-builtin-types.h" '''))I understand why you propose this patch. The QAPI-generated headers use #ifdef
CONFIG_FOO. The configuration header "qemu/osdep.h" must be consistently included before the generated headers, or else you end up with nasty bugs, such as the same enum having different values in different .o, or the same struct having a different layout.Quoted 3 lines
But this applies to *all* headers that use #ifdef. Why check it here, but not there? What makes the QAPI-generated headers special?
Quoted 6 lines
It's the discussion about #if in headers (and enums in particular) that started this. We want to make sure all compilation units share the same data structure/ABI. I proposed to include
osdep.hin qapi headers, but that was rejected.
The warning is a different approach. I agree it could apply to all headers. Do you think I should update all headers as well?That would replace the rule 'all .c files must include "
qemu/osdep.h" first' by 'all .h files must check "qemu/osdep.h" has been included already', which is not an improvement.All we have right now to help with enforcing our
osdep.hrule isscripts/clean-includes. We run it periodically to fix rule breakers. It's manual, because we have a few .c files in the tree where the rule doesn't apply, and the script doesn't know about them. Fixable, I guess. Most recent run:Subject: [PATCH] Clean up includes Message-Id: <[email protected]> https://lists.nongnu.org/archive/html/qemu-devel/2018-12/msg00605.html
Quoted 5 lines
The obvious improvement would be flagging rule breakers before they get committed.
checkpatch.pl could flag patches adding .c files that don't include "
qemu/osdep.h" first. The "first" part might be a bit annoying to code.
You can get this logic from GNULIBs syntax-check make rules. It uses this perl code to check that config.h is included first:
while (<>) {
if (/^# *include\b/) {
if (not m{^# *include <config\.h>}) {
print "$ARGV\n";
$$ret = 1;
}
close ARGV;
}
}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 :|
Re: [Qemu-devel] [PATCH for-4.0 v7 01/27] qapi: make sure osdep.h is included in type headers
Quoted 1 line · Daniel P. Berrangé
On Mon, Dec 10, 2018 at 02:28:26PM +0100, Markus Armbruster wrote:
[...]
Quoted 6 lines
Quoted 2 lines
checkpatch.pl could flag patches adding .c files that don't include "
qemu/osdep.h" first. The "first" part might be a bit annoying to code.You can get this logic from GNULIBs syntax-check make rules. It uses this perl code to check that
config.his included first:
Quoted 9 lines
while (<>) {
if (/^# *include\b/) {
if (not m{^# *include <config\.h>}) {
print "$ARGV\n";
$$ret = 1;
}
close ARGV;
}
}Thanks!
PATCH v7 2/27 qapi: do not define enumeration value explicitly
In reply to Marc-André LureauThe C standard has the initial value at 0 and the subsequent values incremented by 1. No need to set this explicitely.
This will prevent from artificial "gaps" when compiling out some enum values and having unnecessarily large MAX values & enums arrays, or simplifying iterating over valid enum values.
Whenever config-host.h is changed, all the enum/types are recompiled.
--- scripts/qapi/common.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
scripts/qapi/common.py +2 -5
@@ -2045,14 +2045,11 @@ typedef enum %(c_name)s { ''', c_name=c_name(name)) - i = 0 for value in enum_values: ret += mcgen(''' - %(c_enum)s = %(i)d, + %(c_enum)s, ''', - c_enum=c_enum_const(name, value, prefix), - i=i) - i += 1 + c_enum=c_enum_const(name, value, prefix)) ret += mcgen(''' } %(c_name)s;
Signature · 1 line
2.20.0.rc1
Re: [Qemu-devel] [PATCH for-4.0 v7 02/27] qapi: do not define enumeration value explicitly
Quoted 6 lines · Marc-André Lureau
The C standard has the initial value at 0 and the subsequent values incremented by 1. No need to set this explicitely.
This will prevent from artificial "gaps" when compiling out some enum values and having unnecessarily large MAX values & enums arrays, or simplifying iterating over valid enum values.
Should mention that compiling out will only become possible later in this series.
Quoted 1 line
Whenever
config-host.his changed, all the enum/types are recompiled.
This soundness argument is incomplete. Yes, our coding conventions ensure everything gets recompiled when config-host.h changes. But nothing stops people from using 'if' conditions that depend on more than just config-host.h.
What about this:
qapi: Do not define enumeration value explicitly
The generated C enumeration types explicitly set the enumeration constants to 0, 1, 2, ... That's exactly what you get when you don't supply values.
Drop the explicit values. No change now, but it will avoid gaps in the values when we later add support for 'if' conditions. Avoiding such gaps will save us the trouble of changing the ENUM_lookup[] tables to work without a sentinel.
We'll have to take care to ensure the headers required by the 'if' conditions get always included before the generated QAPI code. Fortunately, our convention to include "qemu/osdep.h" first in any .c ensures that's the case for our CONFIG_FOO macros.
Quoted 1 line
Signed-off-by Marc-André Lureau <[email protected]>
With an improved commit message:
Re: [Qemu-devel] [PATCH for-4.0 v7 02/27] qapi: do not define enumeration value explicitly
Hi
Quoted 38 lines · Markus Armbruster
Quoted 6 lines · Marc-André Lureau
The C standard has the initial value at 0 and the subsequent values incremented by 1. No need to set this explicitely.
This will prevent from artificial "gaps" when compiling out some enum values and having unnecessarily large MAX values & enums arrays, or simplifying iterating over valid enum values.
Should mention that compiling out will only become possible later in this series.
Quoted 1 line
Whenever
config-host.his changed, all the enum/types are recompiled.This soundness argument is incomplete. Yes, our coding conventions ensure everything gets recompiled when
config-host.hchanges. But nothing stops people from using 'if' conditions that depend on more than justconfig-host.h.What about this:
qapi: Do not define enumeration value explicitly
The generated C enumeration types explicitly set the enumeration constants to 0, 1, 2, ... That's exactly what you get when you don't supply values.
Drop the explicit values. No change now, but it will avoid gaps in the values when we later add support for 'if' conditions. Avoiding such gaps will save us the trouble of changing the ENUM_lookup[] tables to work without a sentinel.
We'll have to take care to ensure the headers required by the 'if' conditions get always included before the generated QAPI code. Fortunately, our convention to include "
qemu/osdep.h" first in any .c ensures that's the case for ourCONFIG_FOOmacros.
Quoted 4 lines
Quoted 1 line
Signed-off-by Marc-André Lureau <[email protected]>With an improved commit message:
Reviewed-by Markus Armbruster <[email protected]>
ack, thanks!
PATCH v7 3/27 qapi: rename QAPISchemaEnumType.values to .members
In reply to Marc-André LureauRename QAPISchemaEnumType.values and related variables to members. Makes sense ever since commit 93bda4dd4 changed .values from list of string to list of QAPISchemaMember. Obvious no-op.
--- scripts/qapi/common.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
scripts/qapi/common.py +12 -12
@@ -1161,22 +1161,22 @@ class QAPISchemaBuiltinType(QAPISchemaType): class QAPISchemaEnumType(QAPISchemaType): - def __init__(self, name, info, doc, ifcond, values, prefix): + def __init__(self, name, info, doc, ifcond, members, prefix): QAPISchemaType.__init__(self, name, info, doc, ifcond) - for v in values: - assert isinstance(v, QAPISchemaMember) - v.set_owner(name) + for m in members: + assert isinstance(m, QAPISchemaMember) + m.set_owner(name) assert prefix is None or isinstance(prefix, str) - self.values = values + self.members = members self.prefix = prefix def check(self, schema): QAPISchemaType.check(self, schema) seen = {} - for v in self.values: - v.check_clash(self.info, seen) + for m in self.members: + m.check_clash(self.info, seen) if self.doc: - self.doc.connect_member(v) + self.doc.connect_member(m) def is_implicit(self): # See QAPISchema._make_implicit_enum_type() and ._def_predefineds()
@@ -1186,7 +1186,7 @@ class QAPISchemaEnumType(QAPISchemaType): return c_name(self.name) def member_names(self): - return [v.name for v in self.values] + return [m.name for m in self.members] def json_type(self): return 'string'
@@ -1403,9 +1403,9 @@ class QAPISchemaObjectTypeVariants(object): if self._tag_name: # flat union # branches that are not explicitly covered get an empty type cases = set([v.name for v in self.variants]) - for val in self.tag_member.type.values: - if val.name not in cases: - v = QAPISchemaObjectTypeVariant(val.name, 'q_empty') + for m in self.tag_member.type.members: + if m.name not in cases: + v = QAPISchemaObjectTypeVariant(m.name, 'q_empty') v.set_owner(self.tag_member.owner) self.variants.append(v) for v in self.variants:
Signature · 1 line
2.20.0.rc1
PATCH v7 4/27 qapi: change enum visitor and gen_enum* to take QAPISchemaMember
In reply to Marc-André LureauThis will allow to add and access more properties associated with enum values/members, like the associated 'if' condition. We may want to have a specialized type QAPISchemaEnumMember, for now this will do.
Modify gen_enum() and gen_enum_lookup() for the same reason.
--- scripts/qapi/common.py | 22 +++++++++++----------- scripts/qapi/doc.py | 2 +- scripts/qapi/events.py | 13 +++++++------ scripts/qapi/introspect.py | 5 +++-- scripts/qapi/types.py | 6 +++--- scripts/qapi/visit.py | 2 +- tests/qapi-schema/test-qapi.py | 4 ++-- 7 files changed, 28 insertions(+), 26 deletions(-)
scripts/qapi/common.py +11 -11
@@ -1063,7 +1063,7 @@ class QAPISchemaVisitor(object): def visit_builtin_type(self, name, info, json_type): pass - def visit_enum_type(self, name, info, ifcond, values, prefix): + def visit_enum_type(self, name, info, ifcond, members, prefix): pass def visit_array_type(self, name, info, ifcond, element_type):
@@ -1193,7 +1193,7 @@ class QAPISchemaEnumType(QAPISchemaType): def visit(self, visitor): visitor.visit_enum_type(self.name, self.info, self.ifcond, - self.member_names(), self.prefix) + self.members, self.prefix) class QAPISchemaArrayType(QAPISchemaType):
@@ -2012,19 +2012,19 @@ def _wrap_ifcond(ifcond, before, after): return out -def gen_enum_lookup(name, values, prefix=None): +def gen_enum_lookup(name, members, prefix=None): ret = mcgen(''' const QEnumLookup %(c_name)s_lookup = { .array = (const char *const[]) { ''', c_name=c_name(name)) - for value in values: - index = c_enum_const(name, value, prefix) + for m in members: + index = c_enum_const(name, m.name, prefix) ret += mcgen(''' - [%(index)s] = "%(value)s", + [%(index)s] = "%(name)s", ''', - index=index, value=value) + index=index, name=m.name) ret += mcgen(''' },
@@ -2035,9 +2035,9 @@ const QEnumLookup %(c_name)s_lookup = { return ret -def gen_enum(name, values, prefix=None): +def gen_enum(name, members, prefix=None): # append automatically generated _MAX value - enum_values = values + ['_MAX'] + enum_members = members + [QAPISchemaMember('_MAX')] ret = mcgen('''
@@ -2045,11 +2045,11 @@ typedef enum %(c_name)s { ''', c_name=c_name(name)) - for value in enum_values: + for m in enum_members: ret += mcgen(''' %(c_enum)s, ''', - c_enum=c_enum_const(name, value, prefix)) + c_enum=c_enum_const(name, m.name, prefix)) ret += mcgen(''' } %(c_name)s;
scripts/qapi/doc.py +1 -1
@@ -206,7 +206,7 @@ class QAPISchemaGenDocVisitor(qapi.common.QAPISchemaVisitor): def write(self, output_dir): self._gen.write(output_dir, self._prefix + 'qapi-doc.texi') - def visit_enum_type(self, name, info, ifcond, values, prefix): + def visit_enum_type(self, name, info, ifcond, members, prefix): doc = self.cur_doc self._gen.add(TYPE_FMT(type='Enum', name=doc.symbol,
scripts/qapi/events.py +7 -6
@@ -143,8 +143,8 @@ class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor): QAPISchemaModularCVisitor.__init__( self, prefix, 'qapi-events', ' * Schema-defined QAPI/QMP events', __doc__) - self._enum_name = c_name(prefix + 'QAPIEvent', protect=False) - self._event_names = [] + self._event_enum_name = c_name(prefix + 'QAPIEvent', protect=False) + self._event_enum_members = [] def _begin_module(self, name): types = self._module_basename('qapi-types', name)
@@ -170,15 +170,16 @@ class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor): def visit_end(self): (genc, genh) = self._module[self._main_module] - genh.add(gen_enum(self._enum_name, self._event_names)) - genc.add(gen_enum_lookup(self._enum_name, self._event_names)) + genh.add(gen_enum(self._event_enum_name, self._event_enum_members)) + genc.add(gen_enum_lookup(self._event_enum_name, + self._event_enum_members)) def visit_event(self, name, info, ifcond, arg_type, boxed): with ifcontext(ifcond, self._genh, self._genc): self._genh.add(gen_event_send_decl(name, arg_type, boxed)) self._genc.add(gen_event_send(name, arg_type, boxed, - self._enum_name)) - self._event_names.append(name) + self._event_enum_name)) + self._event_enum_members.append(QAPISchemaMember(name)) def gen_events(schema, output_dir, prefix):
scripts/qapi/introspect.py +3 -2
@@ -174,8 +174,9 @@ const QLitObject %(c_name)s = %(c_string)s; def visit_builtin_type(self, name, info, json_type): self._gen_qlit(name, 'builtin', {'json-type': json_type}, []) - def visit_enum_type(self, name, info, ifcond, values, prefix): - self._gen_qlit(name, 'enum', {'values': values}, ifcond) + def visit_enum_type(self, name, info, ifcond, members, prefix): + self._gen_qlit(name, 'enum', + {'values': [m.name for m in members]}, ifcond) def visit_array_type(self, name, info, ifcond, element_type): element = self._use_type(element_type)
scripts/qapi/types.py +3 -3
@@ -215,10 +215,10 @@ class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): self._genh.add(gen_type_cleanup_decl(name)) self._genc.add(gen_type_cleanup(name)) - def visit_enum_type(self, name, info, ifcond, values, prefix): + def visit_enum_type(self, name, info, ifcond, members, prefix): with ifcontext(ifcond, self._genh, self._genc): - self._genh.preamble_add(gen_enum(name, values, prefix)) - self._genc.add(gen_enum_lookup(name, values, prefix)) + self._genh.preamble_add(gen_enum(name, members, prefix)) + self._genc.add(gen_enum_lookup(name, members, prefix)) def visit_array_type(self, name, info, ifcond, element_type): with ifcontext(ifcond, self._genh, self._genc):
scripts/qapi/visit.py +1 -1
@@ -310,7 +310,7 @@ class QAPISchemaGenVisitVisitor(QAPISchemaModularCVisitor): ''', types=types)) - def visit_enum_type(self, name, info, ifcond, values, prefix): + def visit_enum_type(self, name, info, ifcond, members, prefix): with ifcontext(ifcond, self._genh, self._genc): self._genh.add(gen_visit_decl(name, scalar=True)) self._genc.add(gen_visit_enum(name))
tests/qapi-schema/test-qapi.py +2 -2
@@ -23,8 +23,8 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor): def visit_include(self, name, info): print('include %s' % name) - def visit_enum_type(self, name, info, ifcond, values, prefix): - print('enum %s %s' % (name, values)) + def visit_enum_type(self, name, info, ifcond, members, prefix): + print('enum %s %s' % (name, [m.name for m in members])) if prefix: print(' prefix %s' % prefix) self._print_if(ifcond)
Signature · 1 line
2.20.0.rc1
PATCH v7 5/27 tests: print enum type members more like object type members
In reply to Marc-André LureauCommit 93bda4dd461 changed the internal representation of enum type members from str to QAPISchemaMember, but we still print only a string. Has been good enough, as the name is the member's only attribute of interest, but that's about to change. To prepare, print them more like object type members.
--- tests/qapi-schema/comments.out | 14 ++++++- tests/qapi-schema/doc-bad-section.out | 13 +++++- tests/qapi-schema/doc-good.out | 17 ++++++-- tests/qapi-schema/empty.out | 9 ++++- tests/qapi-schema/event-case.out | 9 ++++- tests/qapi-schema/ident-with-escape.out | 9 ++++- tests/qapi-schema/include-relpath.out | 14 ++++++- tests/qapi-schema/include-repetition.out | 14 ++++++- tests/qapi-schema/include-simple.out | 14 ++++++- tests/qapi-schema/indented-expr.out | 9 ++++- tests/qapi-schema/qapi-schema-test.out | 50 +++++++++++++++++++----- tests/qapi-schema/test-qapi.py | 4 +- 12 files changed, 149 insertions(+), 27 deletions(-)
tests/qapi-schema/comments.out +12 -2
@@ -1,5 +1,15 @@ object q_empty -enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool'] +enum QType prefix QTYPE + member none + member qnull + member qnum + member qstring + member qdict + member qlist + member qbool module comments.json -enum Status ['good', 'bad', 'ugly'] +enum Status + member good + member bad + member ugly
tests/qapi-schema/doc-bad-section.out +11 -2
@@ -1,8 +1,17 @@ object q_empty -enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool'] +enum QType prefix QTYPE + member none + member qnull + member qnum + member qstring + member qdict + member qlist + member qbool module doc-bad-section.json -enum Enum ['one', 'two'] +enum Enum + member one + member two doc symbol=Enum body= == Produces *invalid* texinfo
tests/qapi-schema/doc-good.out +14 -3
@@ -1,8 +1,17 @@ object q_empty -enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool'] +enum QType prefix QTYPE + member none + member qnull + member qnum + member qstring + member qdict + member qlist + member qbool module doc-good.json -enum Enum ['one', 'two'] +enum Enum + member one + member two if ['defined(IFCOND)'] object Base member base1: Enum optional=False
@@ -18,7 +27,9 @@ object q_obj_Variant1-wrapper member data: Variant1 optional=False object q_obj_Variant2-wrapper member data: Variant2 optional=False -enum SugaredUnionKind ['one', 'two'] +enum SugaredUnionKind + member one + member two object SugaredUnion member type: SugaredUnionKind optional=False tag type
tests/qapi-schema/empty.out +8 -1
@@ -1,3 +1,10 @@ object q_empty -enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool'] +enum QType prefix QTYPE + member none + member qnull + member qnum + member qstring + member qdict + member qlist + member qbool
tests/qapi-schema/event-case.out +8 -1
@@ -1,6 +1,13 @@ object q_empty -enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool'] +enum QType prefix QTYPE + member none + member qnull + member qnum + member qstring + member qdict + member qlist + member qbool module event-case.json event oops None boxed=False
tests/qapi-schema/ident-with-escape.out +8 -1
@@ -1,6 +1,13 @@ object q_empty -enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool'] +enum QType prefix QTYPE + member none + member qnull + member qnum + member qstring + member qdict + member qlist + member qbool module ident-with-escape.json object q_obj_fooA-arg member bar1: str optional=False
tests/qapi-schema/include-relpath.out +12 -2
@@ -1,9 +1,19 @@ object q_empty -enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool'] +enum QType prefix QTYPE + member none + member qnull + member qnum + member qstring + member qdict + member qlist + member qbool module include-relpath.json include include/relpath.json module include/relpath.json include include-relpath-sub.json module include-relpath-sub.json -enum Status ['good', 'bad', 'ugly'] +enum Status + member good + member bad + member ugly
tests/qapi-schema/include-repetition.out +12 -2
@@ -1,10 +1,20 @@ object q_empty -enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool'] +enum QType prefix QTYPE + member none + member qnull + member qnum + member qstring + member qdict + member qlist + member qbool module include-repetition.json include comments.json module comments.json -enum Status ['good', 'bad', 'ugly'] +enum Status + member good + member bad + member ugly module include-repetition.json include include-repetition-sub.json module include-repetition-sub.json
tests/qapi-schema/include-simple.out +12 -2
@@ -1,7 +1,17 @@ object q_empty -enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool'] +enum QType prefix QTYPE + member none + member qnull + member qnum + member qstring + member qdict + member qlist + member qbool module include-simple.json include include-simple-sub.json module include-simple-sub.json -enum Status ['good', 'bad', 'ugly'] +enum Status + member good + member bad + member ugly
tests/qapi-schema/indented-expr.out +8 -1
@@ -1,6 +1,13 @@ object q_empty -enum QType ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool'] +enum QType prefix QTYPE + member none + member qnull + member qnum + member qstring + member qdict + member qlist + member qbool module indented-expr.json command eins None -> None gen=True success_response=True boxed=False oob=False preconfig=False
tests/qapi-schema/qapi-schema-test.out +41 -9
98 lines not on this page · raw message
tests/qapi-schema/test-qapi.py +3 -1
@@ -24,9 +24,11 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor): print('include %s' % name) def visit_enum_type(self, name, info, ifcond, members, prefix): - print('enum %s %s' % (name, [m.name for m in members])) + print('enum %s' % name) if prefix: print(' prefix %s' % prefix) + for m in members: + print(' member %s' % m.name) self._print_if(ifcond) def visit_object_type(self, name, info, ifcond, base, members, variants):
Signature · 1 line
2.20.0.rc1
PATCH v7 6/27 qapi: factor out checking for keys
In reply to Marc-André LureauIntroduce a new helper function to check if the given keys are known, and if mandatory keys are present. The function will be reused in other places in the following code changes.
--- scripts/qapi/common.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
scripts/qapi/common.py +13 -7
@@ -873,6 +873,17 @@ def check_struct(expr, info): allow_metas=['struct']) +def check_known_keys(info, source, keys, required, optional): + for key in keys: + if key not in required and key not in optional: + raise QAPISemError(info, "Unknown key '%s' in %s" % (key, source)) + + for key in required: + if key not in keys: + raise QAPISemError(info, "Key '%s' is missing from %s" + % (key, source)) + + def check_keys(expr_elem, meta, required, optional=[]): expr = expr_elem['expr'] info = expr_elem['info']
@@ -880,10 +891,9 @@ def check_keys(expr_elem, meta, required, optional=[]): if not isinstance(name, str): raise QAPISemError(info, "'%s' key must have a string value" % meta) required = required + [meta] + source = "%s '%s'" % (meta, name) + check_known_keys(info, source, expr.keys(), required, optional) for (key, value) in expr.items(): - if key not in required and key not in optional: - raise QAPISemError(info, "Unknown key '%s' in %s '%s'" - % (key, meta, name)) if key in ['gen', 'success-response'] and value is not False: raise QAPISemError(info, "'%s' of %s '%s' should only use false value"
@@ -895,10 +905,6 @@ def check_keys(expr_elem, meta, required, optional=[]): % (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'" - % (key, meta, name)) def check_exprs(exprs):
Signature · 1 line
2.20.0.rc1
Re: [Qemu-devel] [PATCH for-4.0 v7 06/27] qapi: factor out checking for keys
Quoted 5 lines · Marc-André Lureau
Introduce a new helper function to check if the given keys are known, and if mandatory keys are present. The function will be reused in other places in the following code changes.
Signed-off-by Marc-André Lureau <[email protected]>
PATCH v7 7/27 qapi: improve reporting of unknown or missing keys
In reply to Marc-André LureauReport the set of missing or unknown keys. And give a hint about the accepted keys.
--- scripts/qapi/common.py | 23 +++++++++++++++-------- tests/qapi-schema/alternate-base.err | 1 + tests/qapi-schema/double-type.err | 1 + tests/qapi-schema/unknown-expr-key.err | 3 ++- tests/qapi-schema/unknown-expr-key.json | 2 +- 5 files changed, 20 insertions(+), 10 deletions(-)
scripts/qapi/common.py +15 -8
@@ -874,14 +874,21 @@ def check_struct(expr, info): def check_known_keys(info, source, keys, required, optional): - for key in keys: - if key not in required and key not in optional: - raise QAPISemError(info, "Unknown key '%s' in %s" % (key, source)) - - for key in required: - if key not in keys: - raise QAPISemError(info, "Key '%s' is missing from %s" - % (key, source)) + + def pprint(elems): + return ', '.join("'" + e + "'" for e in sorted(elems)) + + missing = set(required) - set(keys) + if missing: + raise QAPISemError(info, "Key%s %s %s missing from %s" + % ('s' if len(missing) > 1 else '', pprint(missing), + 'are' if len(missing) > 1 else 'is', source)) + allowed = set(required + optional) + unknown = set(keys) - allowed + if unknown: + raise QAPISemError(info, "Unknown key%s %s in %s\nValid keys are %s." + % ('s' if len(unknown) > 1 else '', pprint(unknown), + source, pprint(allowed))) def check_keys(expr_elem, meta, required, optional=[]):
tests/qapi-schema/alternate-base.err +1 -0
@@ -1 +1,2 @@ tests/qapi-schema/alternate-base.json:4: Unknown key 'base' in alternate 'Alt' +Valid keys are 'alternate', 'data', 'if'.
tests/qapi-schema/double-type.err +1 -0
@@ -1 +1,2 @@ tests/qapi-schema/double-type.json:2: Unknown key 'command' in struct 'bar' +Valid keys are 'base', 'data', 'if', 'struct'.
tests/qapi-schema/unknown-expr-key.err +2 -1
@@ -1 +1,2 @@ -tests/qapi-schema/unknown-expr-key.json:2: Unknown key 'bogus' in struct 'bar' +tests/qapi-schema/unknown-expr-key.json:2: Unknown keys 'bogus', 'phony' in struct 'bar' +Valid keys are 'base', 'data', 'if', 'struct'.
tests/qapi-schema/unknown-expr-key.json +1 -1
@@ -1,2 +1,2 @@ # we reject an expression with unknown top-level keys -{ 'struct': 'bar', 'data': { 'string': 'str'}, 'bogus': { } } +{ 'struct': 'bar', 'data': { 'string': 'str'}, 'bogus': { }, 'phony': { } }
Signature · 1 line
2.20.0.rc1
Re: [Qemu-devel] [PATCH for-4.0 v7 07/27] qapi: improve reporting of unknown or missing keys
Quoted 2 lines · Marc-André Lureau
Report the set of missing or unknown keys. And give a hint about the accepted keys.
Suggest to add:
The error message for multiple meta type members (visible in tests/qapi-schema/double-type.err) is not improved.
Quoted 1 line
Signed-off-by Marc-André Lureau <[email protected]>
Re: [Qemu-devel] [PATCH for-4.0 v7 07/27] qapi: improve reporting of unknown or missing keys
Quoted 6 lines · Markus Armbruster
Quoted 2 lines · Marc-André Lureau
Report the set of missing or unknown keys. And give a hint about the accepted keys.
Quoted 9 lines
Suggest to add:
The error message for multiple meta type members (visible in tests/qapi-schema/double-type.err) is not improved.
Quoted 1 line
Signed-off-by Marc-André Lureau <[email protected]>Reviewed-by Markus Armbruster <[email protected]>
ack, thanks
Signature · 1 line
Marc-André Lureau
PATCH v7 8/27 qapi: add a dictionary form with 'name' key for enum members
In reply to Marc-André LureauDesugar the enum NAME form to { 'name': NAME }. This will allow to add new enum members, such as 'if' in the following patch.
---
scripts/qapi/common.py | 49 ++++++++++++++++---
tests/Makefile.include | 3 +-
tests/qapi-schema/enum-bad-member.err | 1 +
...-dict-member.exit => enum-bad-member.exit} | 0
tests/qapi-schema/enum-bad-member.json | 2 +
...um-dict-member.out => enum-bad-member.out} | 0
.../qapi-schema/enum-dict-member-unknown.err | 2 +
.../qapi-schema/enum-dict-member-unknown.exit | 1 +
.../qapi-schema/enum-dict-member-unknown.json | 2 +
.../qapi-schema/enum-dict-member-unknown.out | 0
tests/qapi-schema/enum-dict-member.err | 1 -
tests/qapi-schema/enum-dict-member.json | 2 -
12 files changed, 52 insertions(+), 11 deletions(-)
create mode 100644 tests/qapi-schema/enum-bad-member.err
rename tests/qapi-schema/{enum-dict-member.exit => enum-bad-member.exit} (100%)
create mode 100644 tests/qapi-schema/enum-bad-member.json
rename tests/qapi-schema/{enum-dict-member.out => enum-bad-member.out} (100%)
create mode 100644 tests/qapi-schema/enum-dict-member-unknown.err
create mode 100644 tests/qapi-schema/enum-dict-member-unknown.exit
create mode 100644 tests/qapi-schema/enum-dict-member-unknown.json
create mode 100644 tests/qapi-schema/enum-dict-member-unknown.out
delete mode 100644 tests/qapi-schema/enum-dict-member.err
delete mode 100644 tests/qapi-schema/enum-dict-member.jsonscripts/qapi/common.py +42 -7
@@ -740,6 +740,10 @@ def check_event(expr, info): allow_metas=meta) +def enum_get_names(expr): + return [e['name'] if isinstance(e, dict) else e for e in expr['data']] + + def check_union(expr, info): name = expr['union'] base = expr.get('base')
@@ -799,7 +803,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_names(enum_define): raise QAPISemError(info, "Discriminator value '%s' is not found in " "enum '%s'"
@@ -831,7 +835,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_names(enum_expr): if v in ['on', 'off']: conflicting.add('QTYPE_QBOOL') if re.match(r'[-+0-9.]', v): # lazy, could be tightened
@@ -847,18 +851,32 @@ def check_alternate(expr, info): types_seen[qt] = key +def normalize_enum(expr): + members = expr['data'] + + # translate short member form to dict form + expr['data'] = [m if isinstance(m, dict) else {'name': m} for m in members] + + def check_enum(expr, info): name = expr['enum'] - members = expr.get('data') + members = expr['data'] prefix = expr.get('prefix') - if not isinstance(members, list): - raise QAPISemError(info, - "Enum '%s' requires an array for 'data'" % name) if prefix is not None and not isinstance(prefix, str): raise QAPISemError(info, "Enum '%s' requires a string for 'prefix'" % name) + + if not isinstance(members, list): + raise QAPISemError(info, + "Enum '%s' requires an array for 'data'" % name) + for member in members: + if isinstance(member, dict): + source = "dictionary member of enum '%s'" % name + check_known_keys(info, source, member, ['name'], []) + member = member['name'] + check_name(info, "Member of enum '%s'" % name, member, enum_member=True)
@@ -1011,6 +1029,15 @@ def check_exprs(exprs): return exprs +def normalize_exprs(exprs): + for expr_elem in exprs: + expr = expr_elem['expr'] + if 'enum' in expr: + normalize_enum(expr) + + return exprs + + # # Schema compiler frontend #
@@ -1567,6 +1594,7 @@ class QAPISchema(object): f = open(fname, 'r') parser = QAPISchemaParser(f) exprs = check_exprs(parser.exprs) + exprs = normalize_exprs(exprs) self.docs = parser.docs self._entity_list = [] self._entity_dict = {}
@@ -1640,7 +1668,14 @@ class QAPISchema(object): qtype_values, 'QTYPE')) def _make_enum_members(self, values): - return [QAPISchemaMember(v) for v in values] + enum = [] + for v in values: + if isinstance(v, dict): + name = v['name'] + else: + name = v + enum.append(QAPISchemaMember(name)) + return enum def _make_implicit_enum_type(self, name, info, ifcond, values): # See also QAPISchemaObjectTypeMember._pretty_owner()
tests/Makefile.include +2 -1
@@ -379,10 +379,11 @@ qapi-schema += double-data.json qapi-schema += double-type.json qapi-schema += duplicate-key.json qapi-schema += empty.json +qapi-schema += enum-bad-member.json 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-unknown.json qapi-schema += enum-int-member.json qapi-schema += enum-member-case.json qapi-schema += enum-missing-data.json
tests/qapi-schema/enum-bad-member.err new file +1 -0
@@ -0,0 +1 @@ +tests/qapi-schema/enum-bad-member.json:2: Member of enum 'MyEnum' requires a string name
tests/qapi-schema/enum-bad-member.exit renamed from tests/qapi-schema/enum-dict-member.exit +0 -0
tests/qapi-schema/enum-bad-member.json new file +2 -0
@@ -0,0 +1,2 @@ +# we reject any enum member that is not a string +{ 'enum': 'MyEnum', 'data': [ [ ] ] }
tests/qapi-schema/enum-bad-member.out renamed from tests/qapi-schema/enum-dict-member.out +0 -0
tests/qapi-schema/enum-dict-member-unknown.err new file +2 -0
@@ -0,0 +1,2 @@ +tests/qapi-schema/enum-dict-member-unknown.json:2: Unknown key 'bad-key' in dictionary member of enum 'MyEnum' +Valid keys are 'name'.
tests/qapi-schema/enum-dict-member-unknown.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/enum-dict-member-unknown.json new file +2 -0
@@ -0,0 +1,2 @@ +# we reject any enum member that is not a string or a dict with 'name' +{ 'enum': 'MyEnum', 'data': [ { 'name': 'foo', 'bad-key': 'str' } ] }
tests/qapi-schema/enum-dict-member-unknown.out new file +0 -0
tests/qapi-schema/enum-dict-member.err deleted +0 -1
@@ -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.json deleted +0 -2
@@ -1,2 +0,0 @@ -# we reject any enum member that is not a string -{ 'enum': 'MyEnum', 'data': [ { 'value': 'str' } ] }
Signature · 1 line
2.20.0.rc1
Re: [Qemu-devel] [PATCH for-4.0 v7 08/27] qapi: add a dictionary form with 'name' key for enum members
Quoted 34 lines · Marc-André Lureau
Desugar the enum NAME form to { 'name': NAME }. This will allow to add new enum members, such as 'if' in the following patch.
Signed-off-by Marc-André Lureau <[email protected]>--- scripts/qapi/common.py | 49 ++++++++++++++++--- tests/Makefile.include | 3 +- tests/qapi-schema/enum-bad-member.err | 1 + ...-dict-member.exit => enum-bad-member.exit} | 0 tests/qapi-schema/enum-bad-member.json | 2 + ...um-dict-member.out => enum-bad-member.out} | 0 .../qapi-schema/enum-dict-member-unknown.err | 2 + .../qapi-schema/enum-dict-member-unknown.exit | 1 + .../qapi-schema/enum-dict-member-unknown.json | 2 + .../qapi-schema/enum-dict-member-unknown.out | 0 tests/qapi-schema/enum-dict-member.err | 1 - tests/qapi-schema/enum-dict-member.json | 2 - 12 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 tests/qapi-schema/enum-bad-member.err rename tests/qapi-schema/{enum-dict-member.exit => enum-bad-member.exit} (100%) create mode 100644 tests/qapi-schema/enum-bad-member.json rename tests/qapi-schema/{enum-dict-member.out => enum-bad-member.out} (100%) create mode 100644 tests/qapi-schema/enum-dict-member-unknown.err create mode 100644 tests/qapi-schema/enum-dict-member-unknown.exit create mode 100644 tests/qapi-schema/enum-dict-member-unknown.json create mode 100644 tests/qapi-schema/enum-dict-member-unknown.out delete mode 100644 tests/qapi-schema/enum-dict-member.err delete mode 100644 tests/qapi-schema/enum-dict-member.jsonscripts/qapi/common.py +0
-0
scripts/qapi/common.py def check_event(expr, info): · line 746
Quoted 7 lines
scripts/qapi/common.py +4
-0@@ -740,6 +740,10 @@ def check_event(expr, info): allow_metas=meta) +def enum_get_names(expr): + return [e['name'] if isinstance(e, dict) else e for e in expr['data']] + +
Slightly more complicated than v6 because it @expr isn't normalized. We'll revisit this at [*] below.
Another way to code this would be
members = normalized_enum_members(e['data']) return [m['type'] for m in members]
where
def normalized_enum_members(members):
[m if isinstance(m, dict) else {'name': m} for m in members]
which then gets reused in normalize_enum()
def normalize_enum(expr)
expr['data'] = normalized_enum_members(expr['data']
Up to you. We could also change it on top.
Quoted 39 lines
scripts/qapi/common.py +10
-4def check_union(expr, info): name = expr['union'] base = expr.get('base')@@ -799,7 +803,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_names(enum_define): raise QAPISemError(info, "Discriminator value '%s' is not found in " "enum '%s'"@@ -831,7 +835,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_names(enum_expr): if v in ['on', 'off']: conflicting.add('QTYPE_QBOOL') if re.match(r'[-+0-9.]', v): # lazy, could be tightened@@ -847,18 +851,32 @@ def check_alternate(expr, info): types_seen[qt] = key +def normalize_enum(expr): + members = expr['data'] + + # translate short member form to dict form + expr['data'] = [m if isinstance(m, dict) else {'name': m} for m in members] + + def check_enum(expr, info): name = expr['enum'] - members = expr.get('data') + members = expr['data'] prefix = expr.get('prefix') - if not isinstance(members, list):
scripts/qapi/common.py def check_alternate(expr, info): · line 873
Quoted 10 lines
scripts/qapi/common.py +5
-2- raise QAPISemError(info, - "Enum '%s' requires an array for 'data'" % name) if prefix is not None and not isinstance(prefix, str): raise QAPISemError(info, "Enum '%s' requires a string for 'prefix'" % name) + + if not isinstance(members, list): + raise QAPISemError(info, + "Enum '%s' requires an array for 'data'" % name) +
Did you move this check down intentionally?
scripts/qapi/common.py def check_alternate(expr, info): · line 881
Quoted 8 lines
scripts/qapi/common.py +5
-0for member in members: + if isinstance(member, dict): + source = "dictionary member of enum '%s'" % name + check_known_keys(info, source, member, ['name'], []) + member = member['name'] + check_name(info, "Member of enum '%s'" % name, member, enum_member=True)
Again, slightly more complicated than v6 because it now runs before normalization. We'll revisit this at [*] below.
Quoted 18 lines
scripts/qapi/common.py +9
-0@@ -1011,6 +1029,15 @@ def check_exprs(exprs): return exprs +def normalize_exprs(exprs): + for expr_elem in exprs: + expr = expr_elem['expr'] + if 'enum' in expr: + normalize_enum(expr) + + return exprs + + # # Schema compiler frontend #
scripts/qapi/common.py class QAPISchema(object): · line 1600
Quoted 7 lines
scripts/qapi/common.py +1
-0@@ -1567,6 +1594,7 @@ class QAPISchema(object): f = open(fname, 'r') parser = QAPISchemaParser(f) exprs = check_exprs(parser.exprs) + exprs = normalize_exprs(exprs) self.docs = parser.docs self._entity_list = [] self._entity_dict = {}
[*] In v6, you normalized at the beginning of check_exprs(). Now, you normalize after check_exprs(). Works, although what I suggested in my review of v6 was to normalize later in check_exprs(), namely right after check_keys(). I believe that would avoid the slight complications I noted above.
scripts/qapi/common.py class QAPISchema(object): · line 1678
Quoted 13 lines
scripts/qapi/common.py +8
-1@@ -1640,7 +1668,14 @@ class QAPISchema(object): qtype_values, 'QTYPE')) def _make_enum_members(self, values): - return [QAPISchemaMember(v) for v in values] + enum = [] + for v in values: + if isinstance(v, dict): + name = v['name'] + else: + name = v + enum.append(QAPISchemaMember(name)) + return enum
Despite normalization to the long form, we still deal with both the long and the short form here, same as in v6. I see you addressed this in new PATCH 11+17. What's wrong with squashing them into this patch?
Quoted 82 lines
scripts/qapi/common.py +0
-0def _make_implicit_enum_type(self, name, info, ifcond, values): # See also QAPISchemaObjectTypeMember._pretty_owner()tests/Makefile.include +2
-1@@ -379,10 +379,11 @@ qapi-schema += double-data.json qapi-schema += double-type.json qapi-schema += duplicate-key.json qapi-schema += empty.json +qapi-schema += enum-bad-member.json 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-unknown.json qapi-schema += enum-int-member.json qapi-schema += enum-member-case.json qapi-schema += enum-missing-data.jsontests/qapi-schema/enum-bad-member.err new file +1
-0@@ -0,0 +1 @@ +tests/qapi-schema/enum-bad-member.json:2: Member of enum 'MyEnum' requires a string nametests/qapi-schema/enum-bad-member.exit renamed from tests/qapi-schema/enum-dict-member.exit +0
-0tests/qapi-schema/enum-bad-member.json new file +2
-0@@ -0,0 +1,2 @@ +# we reject any enum member that is not a string +{ 'enum': 'MyEnum', 'data': [ [ ] ] }tests/qapi-schema/enum-bad-member.out renamed from tests/qapi-schema/enum-dict-member.out +0
-0tests/qapi-schema/enum-dict-member-unknown.err new file +2
-0@@ -0,0 +1,2 @@ +tests/qapi-schema/enum-dict-member-unknown.json:2: Unknown key 'bad-key' in dictionary member of enum 'MyEnum' +Valid keys are 'name'.tests/qapi-schema/enum-dict-member-unknown.exit new file +1
-0@@ -0,0 +1 @@ +1tests/qapi-schema/enum-dict-member-unknown.json new file +2
-0@@ -0,0 +1,2 @@ +# we reject any enum member that is not a string or a dict with 'name' +{ 'enum': 'MyEnum', 'data': [ { 'name': 'foo', 'bad-key': 'str' } ] }tests/qapi-schema/enum-dict-member-unknown.out new file +0
-0tests/qapi-schema/enum-dict-member.err deleted +0
-1@@ -1 +0,0 @@ -tests/qapi-schema/enum-dict-member.json:2: Member of enum 'MyEnum' requires a string nametests/qapi-schema/enum-dict-member.json deleted +0
-0
tests/qapi-schema/enum-dict-member.json line 2
Quoted 2 lines
tests/qapi-schema/enum-dict-member.json deleted +0
-2@@ -1,2 +0,0 @@ -# we reject any enum member that is not a string -{ 'enum': 'MyEnum', 'data': [ { 'value': 'str' } ] }
I think we still need this one to cover "key 'name' is required in the long form".
Re: [Qemu-devel] [PATCH for-4.0 v7 08/27] qapi: add a dictionary form with 'name' key for enum members
One more thing...
Quoted 32 lines · Markus Armbruster
Quoted 30 lines · Marc-André Lureau
Desugar the enum NAME form to { 'name': NAME }. This will allow to add new enum members, such as 'if' in the following patch.
Signed-off-by Marc-André Lureau <[email protected]>--- scripts/qapi/common.py | 49 ++++++++++++++++--- tests/Makefile.include | 3 +- tests/qapi-schema/enum-bad-member.err | 1 + ...-dict-member.exit => enum-bad-member.exit} | 0 tests/qapi-schema/enum-bad-member.json | 2 + ...um-dict-member.out => enum-bad-member.out} | 0 .../qapi-schema/enum-dict-member-unknown.err | 2 + .../qapi-schema/enum-dict-member-unknown.exit | 1 + .../qapi-schema/enum-dict-member-unknown.json | 2 + .../qapi-schema/enum-dict-member-unknown.out | 0 tests/qapi-schema/enum-dict-member.err | 1 - tests/qapi-schema/enum-dict-member.json | 2 - 12 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 tests/qapi-schema/enum-bad-member.err rename tests/qapi-schema/{enum-dict-member.exit => enum-bad-member.exit} (100%) create mode 100644 tests/qapi-schema/enum-bad-member.json rename tests/qapi-schema/{enum-dict-member.out => enum-bad-member.out} (100%) create mode 100644 tests/qapi-schema/enum-dict-member-unknown.err create mode 100644 tests/qapi-schema/enum-dict-member-unknown.exit create mode 100644 tests/qapi-schema/enum-dict-member-unknown.json create mode 100644 tests/qapi-schema/enum-dict-member-unknown.out delete mode 100644 tests/qapi-schema/enum-dict-member.err delete mode 100644 tests/qapi-schema/enum-dict-member.jsonscripts/qapi/common.py +0
-0
[...]
Quoted 40 lines
Quoted 18 lines
scripts/qapi/common.py +8
-2@@ -847,18 +851,32 @@ def check_alternate(expr, info): types_seen[qt] = key +def normalize_enum(expr): + members = expr['data'] + + # translate short member form to dict form + expr['data'] = [m if isinstance(m, dict) else {'name': m} for m in members] + + def check_enum(expr, info): name = expr['enum'] - members = expr.get('data') + members = expr['data'] prefix = expr.get('prefix') - if not isinstance(members, list):scripts/qapi/common.py def check_alternate(expr, info): · line 873
Quoted 10 lines
scripts/qapi/common.py +5
-2- raise QAPISemError(info, - "Enum '%s' requires an array for 'data'" % name) if prefix is not None and not isinstance(prefix, str): raise QAPISemError(info, "Enum '%s' requires a string for 'prefix'" % name) + + if not isinstance(members, list): + raise QAPISemError(info, + "Enum '%s' requires an array for 'data'" % name) +Did you move this check down intentionally?
scripts/qapi/common.py def check_alternate(expr, info): · line 881
Quoted 8 lines
scripts/qapi/common.py +5
-0for member in members: + if isinstance(member, dict): + source = "dictionary member of enum '%s'" % name + check_known_keys(info, source, member, ['name'], []) + member = member['name'] + check_name(info, "Member of enum '%s'" % name, member, enum_member=True)
Quoted 2 lines
Again, slightly more complicated than v6 because it now runs before normalization. We'll revisit this at [*] below.
If we keep it this way, then let's at least avoid assigning to the loop control variable:
member_name = member['name']
else:
member_name = member
check_name(info, "Member of enum '%s'" % name, member_name,
enum_member=True)
[...]
PATCH v7 9/27 qapi: add 'if' to enum members
In reply to Marc-André LureauQAPISchemaMember gains .ifcond for enum members: inherited classes, such as QAPISchemaObjectTypeMember, will thus have an ifcond member after this (those different types will also use the .ifcond to store the condition and generate conditional code in the following patches).
The generated code remains unconditional for now. Later patches generate the conditionals.
--- scripts/qapi/common.py | 10 +++++++--- docs/devel/qapi-code-gen.txt | 9 +++++++++ tests/Makefile.include | 1 + tests/qapi-schema/enum-dict-member-unknown.err | 2 +- 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 | 2 ++ tests/qapi-schema/test-qapi.py | 1 + 11 files changed, 29 insertions(+), 6 deletions(-) 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/common.py +7 -3
@@ -874,7 +874,8 @@ def check_enum(expr, info): for member in members: if isinstance(member, dict): source = "dictionary member of enum '%s'" % name - check_known_keys(info, source, member, ['name'], []) + check_known_keys(info, source, member, ['name'], ['if']) + check_if(member, info) member = member['name'] check_name(info, "Member of enum '%s'" % name, member,
@@ -1358,9 +1359,10 @@ class QAPISchemaObjectType(QAPISchemaType): class QAPISchemaMember(object): role = 'member' - def __init__(self, name): + def __init__(self, name, ifcond=None): assert isinstance(name, str) self.name = name + self.ifcond = listify_cond(ifcond) self.owner = None def set_owner(self, name):
@@ -1672,9 +1674,11 @@ class QAPISchema(object): for v in values: if isinstance(v, dict): name = v['name'] + ifcond = v.get('if') else: name = v - enum.append(QAPISchemaMember(name)) + ifcond = None + enum.append(QAPISchemaMember(name, ifcond)) return enum def _make_implicit_enum_type(self, name, info, ifcond, values):
docs/devel/qapi-code-gen.txt +9 -0
@@ -752,6 +752,15 @@ gets its generated code guarded like this: #endif /* defined(HAVE_BAR) */ #endif /* defined(CONFIG_FOO) */ +An enum value can be replaced by a dictionary with a 'name' and a 'if' +key. + +Example: a conditional 'bar' enum member. + +{ 'enum': 'IfEnum', 'data': + [ 'foo', + { 'name' : 'bar', 'if': 'defined(IFCOND)' } ] } + Please note that you are responsible to ensure that the C code will compile with an arbitrary combination of conditions, since the generators are unable to check it at this point.
tests/Makefile.include +1 -0
@@ -384,6 +384,7 @@ qapi-schema += enum-bad-name.json qapi-schema += enum-bad-prefix.json qapi-schema += enum-clash-member.json qapi-schema += enum-dict-member-unknown.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-unknown.err +1 -1
@@ -1,2 +1,2 @@ tests/qapi-schema/enum-dict-member-unknown.json:2: Unknown key 'bad-key' in dictionary member of enum 'MyEnum' -Valid keys are 'name'. +Valid keys are 'if', 'name'.
tests/qapi-schema/enum-if-invalid.err new file +1 -0
@@ -0,0 +1 @@ +tests/qapi-schema/enum-if-invalid.json:2: 'if' condition must be a string or a list of strings
tests/qapi-schema/enum-if-invalid.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/enum-if-invalid.json new file +3 -0
@@ -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
tests/qapi-schema/qapi-schema-test.json +3 -2
@@ -204,7 +204,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' },
@@ -213,7 +214,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' }, 'returns': 'UserDefThree', 'if': ['defined(TEST_IF_CMD)', 'defined(TEST_IF_STRUCT)'] }
tests/qapi-schema/qapi-schema-test.out +2 -0
@@ -272,6 +272,7 @@ object TestIfStruct enum TestIfEnum member foo member bar + if ['defined(TEST_IF_ENUM_BAR)'] if ['defined(TEST_IF_ENUM)'] object q_obj_TestStruct-wrapper member data: TestStruct optional=False
@@ -290,6 +291,7 @@ alternate TestIfAlternate if ['defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)'] object q_obj_TestIfCmd-arg member foo: TestIfStruct optional=False + member bar: TestIfEnum optional=False if ['defined(TEST_IF_CMD)', 'defined(TEST_IF_STRUCT)'] command TestIfCmd q_obj_TestIfCmd-arg -> UserDefThree gen=True success_response=True boxed=False oob=False preconfig=False
tests/qapi-schema/test-qapi.py +1 -0
@@ -29,6 +29,7 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor): print(' prefix %s' % prefix) for m in members: print(' member %s' % m.name) + self._print_if(m.ifcond, indent=8) self._print_if(ifcond) def visit_object_type(self, name, info, ifcond, base, members, variants):
Signature · 1 line
2.20.0.rc1
PATCH v7 10/27 qapi-events: add 'if' condition to implicit event enum
In reply to Marc-André LureauAdd condition to QAPIEvent enum members based on the event 'if'.
The generated code remains unconditional for now. Later patches generate the conditionals (also there is no additional coverage of this change in qapi-schema-test.out since the event_names enum is an implicit type created by qapi/events.py).
--- scripts/qapi/events.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/qapi/events.py +1 -1
@@ -179,7 +179,7 @@ class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor): self._genh.add(gen_event_send_decl(name, arg_type, boxed)) self._genc.add(gen_event_send(name, arg_type, boxed, self._event_enum_name)) - self._event_enum_members.append(QAPISchemaMember(name)) + self._event_enum_members.append(QAPISchemaMember(name, ifcond)) def gen_events(schema, output_dir, prefix):
Signature · 1 line
2.20.0.rc1
PATCH v7 11/27 qapi: pass long form enum to make_enum_members
In reply to Marc-André LureauThis will allow to get rid of short form handling in a following patch.
--- scripts/qapi/common.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
scripts/qapi/common.py +4 -3
@@ -1663,9 +1663,10 @@ class QAPISchema(object): self.the_empty_object_type = QAPISchemaObjectType( 'q_empty', None, None, None, None, [], None) self._def_entity(self.the_empty_object_type) - qtype_values = self._make_enum_members(['none', 'qnull', 'qnum', - 'qstring', 'qdict', 'qlist', - 'qbool']) + + qtypes = ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool'] + qtype_values = self._make_enum_members([{'name': n} for n in qtypes]) + self._def_entity(QAPISchemaEnumType('QType', None, None, None, qtype_values, 'QTYPE'))
Signature · 1 line
2.20.0.rc1
Re: [Qemu-devel] [PATCH for-4.0 v7 11/27] qapi: pass long form enum to make_enum_members
Quoted 14 lines · Marc-André Lureau
This will allow to get rid of short form handling in a following patch.
Signed-off-by Marc-André Lureau <[email protected]>Suggested-by Markus Armbruster <[email protected]>--- scripts/qapi/common.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)scripts/qapi/common.py +0
-0
scripts/qapi/common.py class QAPISchema(object): · line 1667
Quoted 8 lines
scripts/qapi/common.py +2
-3@@ -1663,9 +1663,10 @@ class QAPISchema(object): self.the_empty_object_type = QAPISchemaObjectType( 'q_empty', None, None, None, None, [], None) self._def_entity(self.the_empty_object_type) - qtype_values = self._make_enum_members(['none', 'qnull', 'qnum', - 'qstring', 'qdict', 'qlist', - 'qbool']) + + qtypes = ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist', 'qbool']
Long line.
scripts/qapi/common.py class QAPISchema(object): · line 1671
Quoted 4 lines
scripts/qapi/common.py +2
-0+ qtype_values = self._make_enum_members([{'name': n} for n in qtypes]) + self._def_entity(QAPISchemaEnumType('QType', None, None, None, qtype_values, 'QTYPE'))
We may want to squash this one into PATCH 08 as mentioned in that patch's review.
In case we don't:
PATCH v7 12/27 qapi: rename allow_dict to allow_implicit
In reply to Marc-André LureauThis makes it a bit clearer what is the intent of the dictionnary for the check_type() function, since there was some confusion on a previous iteration of this series.
--- scripts/qapi/common.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
scripts/qapi/common.py +6 -6
@@ -661,7 +661,7 @@ def check_if(expr, info): def check_type(info, source, value, allow_array=False, - allow_dict=False, allow_optional=False, + allow_implicit=False, allow_optional=False, allow_metas=[]): global all_names
@@ -688,7 +688,7 @@ def check_type(info, source, value, allow_array=False, (source, all_names[value], value)) return - if not allow_dict: + if not allow_implicit: raise QAPISemError(info, "%s should be a type name" % source) if not isinstance(value, OrderedDict):
@@ -718,7 +718,7 @@ def check_command(expr, info): if boxed: args_meta += ['union', 'alternate'] check_type(info, "'data' for command '%s'" % name, - expr.get('data'), allow_dict=not boxed, allow_optional=True, + expr.get('data'), allow_implicit=not boxed, allow_optional=True, allow_metas=args_meta) returns_meta = ['union', 'struct'] if name in returns_whitelist:
@@ -736,7 +736,7 @@ def check_event(expr, info): if boxed: meta += ['union', 'alternate'] check_type(info, "'data' for event '%s'" % name, - expr.get('data'), allow_dict=not boxed, allow_optional=True, + expr.get('data'), allow_implicit=not boxed, allow_optional=True, allow_metas=meta)
@@ -764,7 +764,7 @@ def check_union(expr, info): else: # The object must have a string or dictionary 'base'. check_type(info, "'base' for union '%s'" % name, - base, allow_dict=True, allow_optional=True, + base, allow_implicit=True, allow_optional=True, allow_metas=['struct']) if not base: raise QAPISemError(info, "Flat union '%s' must have a base"
@@ -887,7 +887,7 @@ def check_struct(expr, info): members = expr['data'] check_type(info, "'data' for struct '%s'" % name, members, - allow_dict=True, allow_optional=True) + allow_implicit=True, allow_optional=True) check_type(info, "'base' for struct '%s'" % name, expr.get('base'), allow_metas=['struct'])
Signature · 1 line
2.20.0.rc1
PATCH v7 13/27 qapi: add a dictionary form for TYPE
In reply to Marc-André LureauWherever a struct/union/alternate/command/event member with NAME: TYPE form is accepted, desugar it to a NAME: { 'type': TYPE } form.
This will allow to add new member details, such as 'if' in the following patch to introduce conditionals, or 'default' for default values etc.
--- scripts/qapi/common.py | 71 ++++++++++++++----- tests/Makefile.include | 6 ++ tests/qapi-schema/alternate-invalid-dict.err | 1 + tests/qapi-schema/alternate-invalid-dict.exit | 1 + tests/qapi-schema/alternate-invalid-dict.json | 4 ++ tests/qapi-schema/alternate-invalid-dict.out | 0 .../qapi-schema/event-member-invalid-dict.err | 1 + .../event-member-invalid-dict.exit | 1 + .../event-member-invalid-dict.json | 2 + .../qapi-schema/event-member-invalid-dict.out | 0 tests/qapi-schema/event-nest-struct.json | 2 +- .../flat-union-inline-invalid-dict.err | 1 + .../flat-union-inline-invalid-dict.exit | 1 + .../flat-union-inline-invalid-dict.json | 11 +++ .../flat-union-inline-invalid-dict.out | 0 tests/qapi-schema/flat-union-inline.json | 2 +- .../nested-struct-data-invalid-dict.err | 1 + .../nested-struct-data-invalid-dict.exit | 1 + .../nested-struct-data-invalid-dict.json | 3 + .../nested-struct-data-invalid-dict.out | 0 tests/qapi-schema/nested-struct-data.json | 2 +- tests/qapi-schema/qapi-schema-test.json | 10 +-- .../struct-member-invalid-dict.err | 1 + .../struct-member-invalid-dict.exit | 1 + .../struct-member-invalid-dict.json | 3 + .../struct-member-invalid-dict.out | 0 .../qapi-schema/union-branch-invalid-dict.err | 1 + .../union-branch-invalid-dict.exit | 1 + .../union-branch-invalid-dict.json | 4 ++ .../qapi-schema/union-branch-invalid-dict.out | 0 30 files changed, 106 insertions(+), 26 deletions(-) create mode 100644 tests/qapi-schema/alternate-invalid-dict.err create mode 100644 tests/qapi-schema/alternate-invalid-dict.exit create mode 100644 tests/qapi-schema/alternate-invalid-dict.json create mode 100644 tests/qapi-schema/alternate-invalid-dict.out create mode 100644 tests/qapi-schema/event-member-invalid-dict.err create mode 100644 tests/qapi-schema/event-member-invalid-dict.exit create mode 100644 tests/qapi-schema/event-member-invalid-dict.json create mode 100644 tests/qapi-schema/event-member-invalid-dict.out create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.err create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.exit create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.json create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.out create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.err create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.exit create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.json create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.out create mode 100644 tests/qapi-schema/struct-member-invalid-dict.err create mode 100644 tests/qapi-schema/struct-member-invalid-dict.exit create mode 100644 tests/qapi-schema/struct-member-invalid-dict.json create mode 100644 tests/qapi-schema/struct-member-invalid-dict.out create mode 100644 tests/qapi-schema/union-branch-invalid-dict.err create mode 100644 tests/qapi-schema/union-branch-invalid-dict.exit create mode 100644 tests/qapi-schema/union-branch-invalid-dict.json create mode 100644 tests/qapi-schema/union-branch-invalid-dict.out
scripts/qapi/common.py +53 -18
@@ -588,11 +588,13 @@ def discriminator_find_enum_define(expr): if not base_members: return None - discriminator_type = base_members.get(discriminator) - if not discriminator_type: + discriminator_value = base_members.get(discriminator) + if not discriminator_value: return None - return enum_types.get(discriminator_type) + if isinstance(discriminator_value, dict): + discriminator_value = discriminator_value['type'] + return enum_types.get(discriminator_value) # Names must be letters, numbers, -, and _. They must start with letter,
@@ -660,6 +662,15 @@ def check_if(expr, info): check_if_str(ifcond, info) +def normalize_members(expr, field): + members = expr.get(field) + if isinstance(members, OrderedDict): + for key, arg in members.items(): + if isinstance(arg, dict): + continue + members[key] = {'type': arg} + + def check_type(info, source, value, allow_array=False, allow_implicit=False, allow_optional=False, allow_metas=[]):
@@ -704,8 +715,14 @@ def check_type(info, source, value, allow_array=False, % (source, key)) # Todo: allow dictionaries to represent default values of # an optional argument. - check_type(info, "Member '%s' of %s" % (key, source), arg, - allow_array=True, + if isinstance(arg, dict): + check_known_keys(info, "member '%s' of %s" % (key, source), + arg, ['type'], []) + typ = arg['type'] + else: + typ = arg + check_type(info, "Member '%s' of %s" % (key, source), + typ, allow_array=True, allow_metas=['built-in', 'union', 'alternate', 'struct', 'enum'])
@@ -776,13 +793,15 @@ def check_union(expr, info): # member of the base struct. check_name(info, "Discriminator of flat union '%s'" % name, discriminator) - discriminator_type = base_members.get(discriminator) - if not discriminator_type: + discriminator_value = base_members.get(discriminator) + if not discriminator_value: raise QAPISemError(info, "Discriminator '%s' is not a member of base " "struct '%s'" % (discriminator, base)) - enum_define = enum_types.get(discriminator_type) + if isinstance(discriminator_value, dict): + discriminator_value = discriminator_value['type'] + enum_define = enum_types.get(discriminator_value) allow_metas = ['struct'] # Do not allow string discriminator if not enum_define:
@@ -795,10 +814,16 @@ def check_union(expr, info): raise QAPISemError(info, "Union '%s' cannot have empty 'data'" % name) for (key, value) in members.items(): check_name(info, "Member of union '%s'" % name, key) + if isinstance(value, dict): + check_known_keys(info, "member '%s' of union '%s'" % (key, name), + value, ['type'], []) + typ = value['type'] + else: + typ = value # Each value must name a known type check_type(info, "Member '%s' of union '%s'" % (key, name), - value, allow_array=not base, allow_metas=allow_metas) + typ, allow_array=not base, allow_metas=allow_metas) # If the discriminator names an enum type, then all members # of 'data' must also be members of the enum type.
@@ -822,18 +847,24 @@ def check_alternate(expr, info): "in 'data'" % name) for (key, value) in members.items(): check_name(info, "Member of alternate '%s'" % name, key) + if isinstance(value, dict): + check_known_keys(info, + "member '%s' of alternate '%s'" % (key, name), + value, ['type'], []) + typ = value['type'] + else: + typ = value # Ensure alternates have no type conflicts. - check_type(info, "Member '%s' of alternate '%s'" % (key, name), - value, + check_type(info, "Member '%s' of alternate '%s'" % (key, name), typ, allow_metas=['built-in', 'union', 'struct', 'enum']) - qtype = find_alternate_member_qtype(value) + qtype = find_alternate_member_qtype(typ) if not qtype: raise QAPISemError(info, "Alternate '%s' member '%s' cannot use " - "type '%s'" % (name, key, value)) + "type '%s'" % (name, key, typ)) conflicting = set([qtype]) if qtype == 'QTYPE_QSTRING': - enum_expr = enum_types.get(value) + enum_expr = enum_types.get(typ) if enum_expr: for v in enum_get_names(enum_expr): if v in ['on', 'off']:
@@ -1035,6 +1066,10 @@ def normalize_exprs(exprs): expr = expr_elem['expr'] if 'enum' in expr: normalize_enum(expr) + elif 'union' in expr: + normalize_members(expr, 'base') + if {'union', 'alternate', 'struct', 'command', 'event'} & set(expr): + normalize_members(expr, 'data') return exprs
@@ -1738,7 +1773,7 @@ class QAPISchema(object): return QAPISchemaObjectTypeMember(name, typ, optional) def _make_members(self, data, info): - return [self._make_member(key, value, info) + return [self._make_member(key, value['type'], info) for (key, value) in data.items()] def _def_struct_type(self, expr, info, doc):
@@ -1774,11 +1809,11 @@ class QAPISchema(object): name, info, doc, ifcond, 'base', self._make_members(base, info)) if tag_name: - variants = [self._make_variant(key, value) + variants = [self._make_variant(key, value['type']) for (key, value) in data.items()] members = [] else: - variants = [self._make_simple_variant(key, value, info) + variants = [self._make_simple_variant(key, value['type'], info) for (key, value) in data.items()] typ = self._make_implicit_enum_type(name, info, ifcond, [v.name for v in variants])
@@ -1794,7 +1829,7 @@ class QAPISchema(object): name = expr['alternate'] data = expr['data'] ifcond = expr.get('if') - variants = [self._make_variant(key, value) + variants = [self._make_variant(key, value['type']) for (key, value) in data.items()] tag_member = QAPISchemaObjectTypeMember('type', 'QType', False) self._def_entity(
tests/Makefile.include +6 -0
@@ -318,6 +318,7 @@ qapi-schema += alternate-conflict-string.json qapi-schema += alternate-conflict-bool-string.json qapi-schema += alternate-conflict-num-string.json qapi-schema += alternate-empty.json +qapi-schema += alternate-invalid-dict.json qapi-schema += alternate-nested.json qapi-schema += alternate-unknown.json qapi-schema += args-alternate.json
@@ -394,6 +395,7 @@ qapi-schema += escape-too-big.json qapi-schema += escape-too-short.json qapi-schema += event-boxed-empty.json qapi-schema += event-case.json +qapi-schema += event-member-invalid-dict.json qapi-schema += event-nest-struct.json qapi-schema += flat-union-array-branch.json qapi-schema += flat-union-bad-base.json
@@ -403,6 +405,7 @@ qapi-schema += flat-union-base-union.json qapi-schema += flat-union-clash-member.json qapi-schema += flat-union-empty.json qapi-schema += flat-union-inline.json +qapi-schema += flat-union-inline-invalid-dict.json qapi-schema += flat-union-int-branch.json qapi-schema += flat-union-invalid-branch-key.json qapi-schema += flat-union-invalid-discriminator.json
@@ -430,6 +433,7 @@ qapi-schema += missing-comma-list.json qapi-schema += missing-comma-object.json qapi-schema += missing-type.json qapi-schema += nested-struct-data.json +qapi-schema += nested-struct-data-invalid-dict.json qapi-schema += non-objects.json qapi-schema += oob-test.json qapi-schema += allow-preconfig-test.json
@@ -460,6 +464,7 @@ 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-member-invalid-dict.json qapi-schema += struct-member-invalid.json qapi-schema += trailing-comma-list.json qapi-schema += trailing-comma-object.json
@@ -471,6 +476,7 @@ qapi-schema += unicode-str.json qapi-schema += union-base-empty.json qapi-schema += union-base-no-discriminator.json qapi-schema += union-branch-case.json +qapi-schema += union-branch-invalid-dict.json qapi-schema += union-clash-branches.json qapi-schema += union-empty.json qapi-schema += union-invalid-base.json
tests/qapi-schema/alternate-invalid-dict.err new file +1 -0
@@ -0,0 +1 @@ +tests/qapi-schema/alternate-invalid-dict.json:2: Key 'type' is missing from member 'two' of alternate 'Alt'
tests/qapi-schema/alternate-invalid-dict.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/alternate-invalid-dict.json new file +4 -0
@@ -0,0 +1,4 @@ +# exploded member form must have a 'type' +{ 'alternate': 'Alt', + 'data': { 'one': 'str', + 'two': { 'if': 'foo' } } }
tests/qapi-schema/alternate-invalid-dict.out new file +0 -0
tests/qapi-schema/event-member-invalid-dict.err new file +1 -0
@@ -0,0 +1 @@ +tests/qapi-schema/event-member-invalid-dict.json:1: Key 'type' is missing from member 'a' of 'data' for event 'EVENT_A'
tests/qapi-schema/event-member-invalid-dict.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/event-member-invalid-dict.json new file +2 -0
@@ -0,0 +1,2 @@ +{ 'event': 'EVENT_A', + 'data': { 'a' : { 'string' : 'str', 'integer': 'int' }, 'b' : 'str' } }
tests/qapi-schema/event-member-invalid-dict.out new file +0 -0
tests/qapi-schema/event-nest-struct.json +1 -1
@@ -1,2 +1,2 @@ { 'event': 'EVENT_A', - 'data': { 'a' : { 'string' : 'str', 'integer': 'int' }, 'b' : 'str' } } + 'data': { 'a' : { 'type' : { 'integer': 'int' } }, 'b' : 'str' } }
tests/qapi-schema/flat-union-inline-invalid-dict.err new file +1 -0
@@ -0,0 +1 @@ +tests/qapi-schema/flat-union-inline-invalid-dict.json:7: Key 'type' is missing from member 'value1' of union 'TestUnion'
tests/qapi-schema/flat-union-inline-invalid-dict.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/flat-union-inline-invalid-dict.json new file +11 -0
@@ -0,0 +1,11 @@ +# we require branches to be a struct name +# TODO: should we allow anonymous inline branch types? +{ 'enum': 'TestEnum', + 'data': [ 'value1', 'value2' ] } +{ 'struct': 'Base', + 'data': { 'enum1': 'TestEnum', 'kind': 'str' } } +{ 'union': 'TestUnion', + 'base': 'Base', + 'discriminator': 'enum1', + 'data': { 'value1': { 'string': 'str' }, + 'value2': { 'integer': 'int' } } }
tests/qapi-schema/flat-union-inline-invalid-dict.out new file +0 -0
tests/qapi-schema/flat-union-inline.json +1 -1
@@ -7,5 +7,5 @@ { 'union': 'TestUnion', 'base': 'Base', 'discriminator': 'enum1', - 'data': { 'value1': { 'string': 'str' }, + 'data': { 'value1': { 'type': {} }, 'value2': { 'integer': 'int' } } }
tests/qapi-schema/nested-struct-data-invalid-dict.err new file +1 -0
@@ -0,0 +1 @@ +tests/qapi-schema/nested-struct-data-invalid-dict.json:2: Key 'type' is missing from member 'a' of 'data' for command 'foo'
tests/qapi-schema/nested-struct-data-invalid-dict.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/nested-struct-data-invalid-dict.json new file +3 -0
@@ -0,0 +1,3 @@ +# inline subtypes collide with our desired future use of defaults +{ 'command': 'foo', + 'data': { 'a' : { 'string' : 'str', 'integer': 'int' }, 'b' : 'str' } }
tests/qapi-schema/nested-struct-data-invalid-dict.out new file +0 -0
tests/qapi-schema/nested-struct-data.json +1 -1
@@ -1,3 +1,3 @@ # inline subtypes collide with our desired future use of defaults { 'command': 'foo', - 'data': { 'a' : { 'string' : 'str', 'integer': 'int' }, 'b' : 'str' } } + 'data': { 'a' : { 'type': {} }, 'b' : 'str' } }
tests/qapi-schema/qapi-schema-test.json +5 -5
@@ -11,7 +11,7 @@ 'guest-sync' ] } } { 'struct': 'TestStruct', - 'data': { 'integer': 'int', 'boolean': 'bool', 'string': 'str' } } + 'data': { 'integer': {'type': 'int'}, 'boolean': 'bool', 'string': 'str' } } # for testing enums { 'struct': 'NestedEnumsOne',
@@ -77,7 +77,7 @@ { 'union': 'UserDefFlatUnion', 'base': 'UserDefUnionBase', # intentional forward reference 'discriminator': 'enum1', - 'data': { 'value1' : 'UserDefA', + 'data': { 'value1' : {'type': 'UserDefA'}, 'value2' : 'UserDefB', 'value3' : 'UserDefB' # 'value4' defaults to empty
@@ -98,7 +98,7 @@ { 'struct': 'WrapAlternate', 'data': { 'alt': 'UserDefAlternate' } } { 'alternate': 'UserDefAlternate', - 'data': { 'udfu': 'UserDefFlatUnion', 'e': 'EnumOne', 'i': 'int', + 'data': { 'udfu': {'type': 'UserDefFlatUnion'}, 'e': 'EnumOne', 'i': 'int', 'n': 'null' } } { 'struct': 'UserDefC',
@@ -134,7 +134,7 @@ { 'command': 'user_def_cmd', 'data': {} } { 'command': 'user_def_cmd1', 'data': {'ud1a': 'UserDefOne'} } { 'command': 'user_def_cmd2', - 'data': {'ud1a': 'UserDefOne', '*ud1b': 'UserDefOne'}, + 'data': {'ud1a': {'type': 'UserDefOne'}, '*ud1b': 'UserDefOne'}, 'returns': 'UserDefTwo' } { 'command': 'cmd-success-response', 'data': {}, 'success-response': false }
@@ -166,7 +166,7 @@ # testing event { 'struct': 'EventStructOne', - 'data': { 'struct1': 'UserDefOne', 'string': 'str', '*enum2': 'EnumOne' } } + 'data': { 'struct1': {'type': 'UserDefOne'}, 'string': 'str', '*enum2': 'EnumOne' } } { 'event': 'EVENT_A' } { 'event': 'EVENT_B',
tests/qapi-schema/struct-member-invalid-dict.err new file +1 -0
@@ -0,0 +1 @@ +tests/qapi-schema/struct-member-invalid-dict.json:2: Key 'type' is missing from member '*a' of 'data' for struct 'foo'
tests/qapi-schema/struct-member-invalid-dict.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/struct-member-invalid-dict.json new file +3 -0
@@ -0,0 +1,3 @@ +# Long form of member must have a value member 'type' +{ 'struct': 'foo', + 'data': { '*a': { 'case': 'foo' } } }
tests/qapi-schema/struct-member-invalid-dict.out new file +0 -0
tests/qapi-schema/union-branch-invalid-dict.err new file +1 -0
@@ -0,0 +1 @@ +tests/qapi-schema/union-branch-invalid-dict.json:2: Key 'type' is missing from member 'integer' of union 'UnionInvalidBranch'
tests/qapi-schema/union-branch-invalid-dict.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/union-branch-invalid-dict.json new file +4 -0
@@ -0,0 +1,4 @@ +# Long form of member must have a value member 'type' +{ 'union': 'UnionInvalidBranch', + 'data': { 'integer': { 'if': 'foo'}, + 's8': 'int8' } }
tests/qapi-schema/union-branch-invalid-dict.out new file +0 -0
Signature · 1 line
2.20.0.rc1
Re: [Qemu-devel] [PATCH for-4.0 v7 13/27] qapi: add a dictionary form for TYPE
Quoted 73 lines · Marc-André Lureau
Wherever a struct/union/alternate/command/event member with NAME: TYPE form is accepted, desugar it to a NAME: { 'type': TYPE } form.
This will allow to add new member details, such as 'if' in the following patch to introduce conditionals, or 'default' for default values etc.
Signed-off-by Marc-André Lureau <[email protected]>--- scripts/qapi/common.py | 71 ++++++++++++++----- tests/Makefile.include | 6 ++ tests/qapi-schema/alternate-invalid-dict.err | 1 + tests/qapi-schema/alternate-invalid-dict.exit | 1 + tests/qapi-schema/alternate-invalid-dict.json | 4 ++ tests/qapi-schema/alternate-invalid-dict.out | 0 .../qapi-schema/event-member-invalid-dict.err | 1 + .../event-member-invalid-dict.exit | 1 + .../event-member-invalid-dict.json | 2 + .../qapi-schema/event-member-invalid-dict.out | 0 tests/qapi-schema/event-nest-struct.json | 2 +- .../flat-union-inline-invalid-dict.err | 1 + .../flat-union-inline-invalid-dict.exit | 1 + .../flat-union-inline-invalid-dict.json | 11 +++ .../flat-union-inline-invalid-dict.out | 0 tests/qapi-schema/flat-union-inline.json | 2 +- .../nested-struct-data-invalid-dict.err | 1 + .../nested-struct-data-invalid-dict.exit | 1 + .../nested-struct-data-invalid-dict.json | 3 + .../nested-struct-data-invalid-dict.out | 0 tests/qapi-schema/nested-struct-data.json | 2 +- tests/qapi-schema/qapi-schema-test.json | 10 +-- .../struct-member-invalid-dict.err | 1 + .../struct-member-invalid-dict.exit | 1 + .../struct-member-invalid-dict.json | 3 + .../struct-member-invalid-dict.out | 0 .../qapi-schema/union-branch-invalid-dict.err | 1 + .../union-branch-invalid-dict.exit | 1 + .../union-branch-invalid-dict.json | 4 ++ .../qapi-schema/union-branch-invalid-dict.out | 0 30 files changed, 106 insertions(+), 26 deletions(-) create mode 100644 tests/qapi-schema/alternate-invalid-dict.err create mode 100644 tests/qapi-schema/alternate-invalid-dict.exit create mode 100644 tests/qapi-schema/alternate-invalid-dict.json create mode 100644 tests/qapi-schema/alternate-invalid-dict.out create mode 100644 tests/qapi-schema/event-member-invalid-dict.err create mode 100644 tests/qapi-schema/event-member-invalid-dict.exit create mode 100644 tests/qapi-schema/event-member-invalid-dict.json create mode 100644 tests/qapi-schema/event-member-invalid-dict.out create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.err create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.exit create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.json create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.out create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.err create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.exit create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.json create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.out create mode 100644 tests/qapi-schema/struct-member-invalid-dict.err create mode 100644 tests/qapi-schema/struct-member-invalid-dict.exit create mode 100644 tests/qapi-schema/struct-member-invalid-dict.json create mode 100644 tests/qapi-schema/struct-member-invalid-dict.out create mode 100644 tests/qapi-schema/union-branch-invalid-dict.err create mode 100644 tests/qapi-schema/union-branch-invalid-dict.exit create mode 100644 tests/qapi-schema/union-branch-invalid-dict.json create mode 100644 tests/qapi-schema/union-branch-invalid-dict.outscripts/qapi/common.py +0
-0@@ -588,11 +588,13 @@ def discriminator_find_enum_define(expr): if not base_members: return None
scripts/qapi/common.py def discriminator_find_enum_define(expr): · line 597
Quoted 10 lines
scripts/qapi/common.py +5
-3- discriminator_type = base_members.get(discriminator) - if not discriminator_type: + discriminator_value = base_members.get(discriminator) + if not discriminator_value: return None - return enum_types.get(discriminator_type) + if isinstance(discriminator_value, dict): + discriminator_value = discriminator_value['type'] + return enum_types.get(discriminator_value)
As in PATCH 08, this is slightly more complicated than v6 because @expr isn't normalized. I can't say offhand whether normalizing in the place I suggested in my review of v6 would avoid this complication.
Quoted 26 lines
scripts/qapi/common.py +10
-2# Names must be letters, numbers, -, and _. They must start with letter,@@ -660,6 +662,15 @@ def check_if(expr, info): check_if_str(ifcond, info) +def normalize_members(expr, field): + members = expr.get(field) + if isinstance(members, OrderedDict): + for key, arg in members.items(): + if isinstance(arg, dict): + continue + members[key] = {'type': arg} + + def check_type(info, source, value, allow_array=False, allow_implicit=False, allow_optional=False, allow_metas=[]):@@ -704,8 +715,14 @@ def check_type(info, source, value, allow_array=False, % (source, key)) # Todo: allow dictionaries to represent default values of # an optional argument. - check_type(info, "Member '%s' of %s" % (key, source), arg, - allow_array=True, + if isinstance(arg, dict):
scripts/qapi/common.py def check_type(info, source, value, allow_array=False, · line 728
Quoted 10 lines
scripts/qapi/common.py +7
-0+ check_known_keys(info, "member '%s' of %s" % (key, source), + arg, ['type'], []) + typ = arg['type'] + else: + typ = arg + check_type(info, "Member '%s' of %s" % (key, source), + typ, allow_array=True, allow_metas=['built-in', 'union', 'alternate', 'struct', 'enum'])
Slightly more complicated than v6, this time because members of the implicit struct type aren't normalized, yet.
Quoted 20 lines
scripts/qapi/common.py +5
-3@@ -776,13 +793,15 @@ def check_union(expr, info): # member of the base struct. check_name(info, "Discriminator of flat union '%s'" % name, discriminator) - discriminator_type = base_members.get(discriminator) - if not discriminator_type: + discriminator_value = base_members.get(discriminator) + if not discriminator_value: raise QAPISemError(info, "Discriminator '%s' is not a member of base " "struct '%s'" % (discriminator, base)) - enum_define = enum_types.get(discriminator_type) + if isinstance(discriminator_value, dict): + discriminator_value = discriminator_value['type'] + enum_define = enum_types.get(discriminator_value) allow_metas = ['struct'] # Do not allow string discriminator if not enum_define:
scripts/qapi/common.py def check_union(expr, info): · line 822
Quoted 9 lines
scripts/qapi/common.py +6
-0@@ -795,10 +814,16 @@ def check_union(expr, info): raise QAPISemError(info, "Union '%s' cannot have empty 'data'" % name) for (key, value) in members.items(): check_name(info, "Member of union '%s'" % name, key) + if isinstance(value, dict): + check_known_keys(info, "member '%s' of union '%s'" % (key, name), + value, ['type'], []) + typ = value['type'] + else: + typ = value
Likewise.
Quoted 9 lines
scripts/qapi/common.py +1
-1# Each value must name a known type check_type(info, "Member '%s' of union '%s'" % (key, name), - value, allow_array=not base, allow_metas=allow_metas) + typ, allow_array=not base, allow_metas=allow_metas) # If the discriminator names an enum type, then all members # of 'data' must also be members of the enum type.
scripts/qapi/common.py def check_alternate(expr, info): · line 856
Quoted 10 lines
scripts/qapi/common.py +7
-0@@ -822,18 +847,24 @@ def check_alternate(expr, info): "in 'data'" % name) for (key, value) in members.items(): check_name(info, "Member of alternate '%s'" % name, key) + if isinstance(value, dict): + check_known_keys(info, + "member '%s' of alternate '%s'" % (key, name), + value, ['type'], []) + typ = value['type'] + else: + typ = value
Likewise.
tests/qapi-schema/union-branch-invalid-dict.out
Quoted 356 lines
scripts/qapi/common.py +12
-9# Ensure alternates have no type conflicts. - check_type(info, "Member '%s' of alternate '%s'" % (key, name), - value, + check_type(info, "Member '%s' of alternate '%s'" % (key, name), typ, allow_metas=['built-in', 'union', 'struct', 'enum']) - qtype = find_alternate_member_qtype(value) + qtype = find_alternate_member_qtype(typ) if not qtype: raise QAPISemError(info, "Alternate '%s' member '%s' cannot use " - "type '%s'" % (name, key, value)) + "type '%s'" % (name, key, typ)) conflicting = set([qtype]) if qtype == 'QTYPE_QSTRING': - enum_expr = enum_types.get(value) + enum_expr = enum_types.get(typ) if enum_expr: for v in enum_get_names(enum_expr): if v in ['on', 'off']:@@ -1035,6 +1066,10 @@ def normalize_exprs(exprs): expr = expr_elem['expr'] if 'enum' in expr: normalize_enum(expr) + elif 'union' in expr: + normalize_members(expr, 'base') + if {'union', 'alternate', 'struct', 'command', 'event'} & set(expr): + normalize_members(expr, 'data') return exprs@@ -1738,7 +1773,7 @@ class QAPISchema(object): return QAPISchemaObjectTypeMember(name, typ, optional) def _make_members(self, data, info): - return [self._make_member(key, value, info) + return [self._make_member(key, value['type'], info) for (key, value) in data.items()] def _def_struct_type(self, expr, info, doc):@@ -1774,11 +1809,11 @@ class QAPISchema(object): name, info, doc, ifcond, 'base', self._make_members(base, info)) if tag_name: - variants = [self._make_variant(key, value) + variants = [self._make_variant(key, value['type']) for (key, value) in data.items()] members = [] else: - variants = [self._make_simple_variant(key, value, info) + variants = [self._make_simple_variant(key, value['type'], info) for (key, value) in data.items()] typ = self._make_implicit_enum_type(name, info, ifcond, [v.name for v in variants])@@ -1794,7 +1829,7 @@ class QAPISchema(object): name = expr['alternate'] data = expr['data'] ifcond = expr.get('if') - variants = [self._make_variant(key, value) + variants = [self._make_variant(key, value['type']) for (key, value) in data.items()] tag_member = QAPISchemaObjectTypeMember('type', 'QType', False) self._def_entity(tests/Makefile.include +6
-0@@ -318,6 +318,7 @@ qapi-schema += alternate-conflict-string.json qapi-schema += alternate-conflict-bool-string.json qapi-schema += alternate-conflict-num-string.json qapi-schema += alternate-empty.json +qapi-schema += alternate-invalid-dict.json qapi-schema += alternate-nested.json qapi-schema += alternate-unknown.json qapi-schema += args-alternate.json@@ -394,6 +395,7 @@ qapi-schema += escape-too-big.json qapi-schema += escape-too-short.json qapi-schema += event-boxed-empty.json qapi-schema += event-case.json +qapi-schema += event-member-invalid-dict.json qapi-schema += event-nest-struct.json qapi-schema += flat-union-array-branch.json qapi-schema += flat-union-bad-base.json@@ -403,6 +405,7 @@ qapi-schema += flat-union-base-union.json qapi-schema += flat-union-clash-member.json qapi-schema += flat-union-empty.json qapi-schema += flat-union-inline.json +qapi-schema += flat-union-inline-invalid-dict.json qapi-schema += flat-union-int-branch.json qapi-schema += flat-union-invalid-branch-key.json qapi-schema += flat-union-invalid-discriminator.json@@ -430,6 +433,7 @@ qapi-schema += missing-comma-list.json qapi-schema += missing-comma-object.json qapi-schema += missing-type.json qapi-schema += nested-struct-data.json +qapi-schema += nested-struct-data-invalid-dict.json qapi-schema += non-objects.json qapi-schema += oob-test.json qapi-schema += allow-preconfig-test.json@@ -460,6 +464,7 @@ 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-member-invalid-dict.json qapi-schema += struct-member-invalid.json qapi-schema += trailing-comma-list.json qapi-schema += trailing-comma-object.json@@ -471,6 +476,7 @@ qapi-schema += unicode-str.json qapi-schema += union-base-empty.json qapi-schema += union-base-no-discriminator.json qapi-schema += union-branch-case.json +qapi-schema += union-branch-invalid-dict.json qapi-schema += union-clash-branches.json qapi-schema += union-empty.json qapi-schema += union-invalid-base.jsontests/qapi-schema/alternate-invalid-dict.err new file +1
-0@@ -0,0 +1 @@ +tests/qapi-schema/alternate-invalid-dict.json:2: Key 'type' is missing from member 'two' of alternate 'Alt'tests/qapi-schema/alternate-invalid-dict.exit new file +1
-0@@ -0,0 +1 @@ +1tests/qapi-schema/alternate-invalid-dict.json new file +4
-0@@ -0,0 +1,4 @@ +# exploded member form must have a 'type' +{ 'alternate': 'Alt', + 'data': { 'one': 'str', + 'two': { 'if': 'foo' } } }tests/qapi-schema/alternate-invalid-dict.out new file +0
-0tests/qapi-schema/event-member-invalid-dict.err new file +1
-0@@ -0,0 +1 @@ +tests/qapi-schema/event-member-invalid-dict.json:1: Key 'type' is missing from member 'a' of 'data' for event 'EVENT_A'tests/qapi-schema/event-member-invalid-dict.exit new file +1
-0@@ -0,0 +1 @@ +1tests/qapi-schema/event-member-invalid-dict.json new file +2
-0@@ -0,0 +1,2 @@ +{ 'event': 'EVENT_A', + 'data': { 'a' : { 'string' : 'str', 'integer': 'int' }, 'b' : 'str' } }tests/qapi-schema/event-member-invalid-dict.out new file +0
-0tests/qapi-schema/event-nest-struct.json +1
-1@@ -1,2 +1,2 @@ { 'event': 'EVENT_A', - 'data': { 'a' : { 'string' : 'str', 'integer': 'int' }, 'b' : 'str' } } + 'data': { 'a' : { 'type' : { 'integer': 'int' } }, 'b' : 'str' } }tests/qapi-schema/flat-union-inline-invalid-dict.err new file +1
-0@@ -0,0 +1 @@ +tests/qapi-schema/flat-union-inline-invalid-dict.json:7: Key 'type' is missing from member 'value1' of union 'TestUnion'tests/qapi-schema/flat-union-inline-invalid-dict.exit new file +1
-0@@ -0,0 +1 @@ +1tests/qapi-schema/flat-union-inline-invalid-dict.json new file +11
-0@@ -0,0 +1,11 @@ +# we require branches to be a struct name +# TODO: should we allow anonymous inline branch types? +{ 'enum': 'TestEnum', + 'data': [ 'value1', 'value2' ] } +{ 'struct': 'Base', + 'data': { 'enum1': 'TestEnum', 'kind': 'str' } } +{ 'union': 'TestUnion', + 'base': 'Base', + 'discriminator': 'enum1', + 'data': { 'value1': { 'string': 'str' }, + 'value2': { 'integer': 'int' } } }tests/qapi-schema/flat-union-inline-invalid-dict.out new file +0
-0tests/qapi-schema/flat-union-inline.json +1
-1@@ -7,5 +7,5 @@ { 'union': 'TestUnion', 'base': 'Base', 'discriminator': 'enum1', - 'data': { 'value1': { 'string': 'str' }, + 'data': { 'value1': { 'type': {} }, 'value2': { 'integer': 'int' } } }tests/qapi-schema/nested-struct-data-invalid-dict.err new file +1
-0@@ -0,0 +1 @@ +tests/qapi-schema/nested-struct-data-invalid-dict.json:2: Key 'type' is missing from member 'a' of 'data' for command 'foo'tests/qapi-schema/nested-struct-data-invalid-dict.exit new file +1
-0@@ -0,0 +1 @@ +1tests/qapi-schema/nested-struct-data-invalid-dict.json new file +3
-0@@ -0,0 +1,3 @@ +# inline subtypes collide with our desired future use of defaults +{ 'command': 'foo', + 'data': { 'a' : { 'string' : 'str', 'integer': 'int' }, 'b' : 'str' } }tests/qapi-schema/nested-struct-data-invalid-dict.out new file +0
-0tests/qapi-schema/nested-struct-data.json +1
-1@@ -1,3 +1,3 @@ # inline subtypes collide with our desired future use of defaults { 'command': 'foo', - 'data': { 'a' : { 'string' : 'str', 'integer': 'int' }, 'b' : 'str' } } + 'data': { 'a' : { 'type': {} }, 'b' : 'str' } }tests/qapi-schema/qapi-schema-test.json +5
-5@@ -11,7 +11,7 @@ 'guest-sync' ] } } { 'struct': 'TestStruct', - 'data': { 'integer': 'int', 'boolean': 'bool', 'string': 'str' } } + 'data': { 'integer': {'type': 'int'}, 'boolean': 'bool', 'string': 'str' } } # for testing enums { 'struct': 'NestedEnumsOne',@@ -77,7 +77,7 @@ { 'union': 'UserDefFlatUnion', 'base': 'UserDefUnionBase', # intentional forward reference 'discriminator': 'enum1', - 'data': { 'value1' : 'UserDefA', + 'data': { 'value1' : {'type': 'UserDefA'}, 'value2' : 'UserDefB', 'value3' : 'UserDefB' # 'value4' defaults to empty@@ -98,7 +98,7 @@ { 'struct': 'WrapAlternate', 'data': { 'alt': 'UserDefAlternate' } } { 'alternate': 'UserDefAlternate', - 'data': { 'udfu': 'UserDefFlatUnion', 'e': 'EnumOne', 'i': 'int', + 'data': { 'udfu': {'type': 'UserDefFlatUnion'}, 'e': 'EnumOne', 'i': 'int', 'n': 'null' } } { 'struct': 'UserDefC',@@ -134,7 +134,7 @@ { 'command': 'user_def_cmd', 'data': {} } { 'command': 'user_def_cmd1', 'data': {'ud1a': 'UserDefOne'} } { 'command': 'user_def_cmd2', - 'data': {'ud1a': 'UserDefOne', '*ud1b': 'UserDefOne'}, + 'data': {'ud1a': {'type': 'UserDefOne'}, '*ud1b': 'UserDefOne'}, 'returns': 'UserDefTwo' } { 'command': 'cmd-success-response', 'data': {}, 'success-response': false }@@ -166,7 +166,7 @@ # testing event { 'struct': 'EventStructOne', - 'data': { 'struct1': 'UserDefOne', 'string': 'str', '*enum2': 'EnumOne' } } + 'data': { 'struct1': {'type': 'UserDefOne'}, 'string': 'str', '*enum2': 'EnumOne' } } { 'event': 'EVENT_A' } { 'event': 'EVENT_B',tests/qapi-schema/struct-member-invalid-dict.err new file +1
-0@@ -0,0 +1 @@ +tests/qapi-schema/struct-member-invalid-dict.json:2: Key 'type' is missing from member '*a' of 'data' for struct 'foo'tests/qapi-schema/struct-member-invalid-dict.exit new file +1
-0@@ -0,0 +1 @@ +1tests/qapi-schema/struct-member-invalid-dict.json new file +3
-0@@ -0,0 +1,3 @@ +# Long form of member must have a value member 'type' +{ 'struct': 'foo', + 'data': { '*a': { 'case': 'foo' } } }tests/qapi-schema/struct-member-invalid-dict.out new file +0
-0tests/qapi-schema/union-branch-invalid-dict.err new file +1
-0@@ -0,0 +1 @@ +tests/qapi-schema/union-branch-invalid-dict.json:2: Key 'type' is missing from member 'integer' of union 'UnionInvalidBranch'tests/qapi-schema/union-branch-invalid-dict.exit new file +1
-0@@ -0,0 +1 @@ +1tests/qapi-schema/union-branch-invalid-dict.json new file +4
-0@@ -0,0 +1,4 @@ +# Long form of member must have a value member 'type' +{ 'union': 'UnionInvalidBranch', + 'data': { 'integer': { 'if': 'foo'}, + 's8': 'int8' } }tests/qapi-schema/union-branch-invalid-dict.out new file +0
-0
Looks good, but I'd like to investigate whether my suggested placement of the normalization would simplify things before I give my R-by.
Re: [Qemu-devel] [PATCH for-4.0 v7 13/27] qapi: add a dictionary form for TYPE
Hi
Quoted 536 lines · Markus Armbruster
Quoted 73 lines · Marc-André Lureau
Wherever a struct/union/alternate/command/event member with NAME: TYPE form is accepted, desugar it to a NAME: { 'type': TYPE } form.
This will allow to add new member details, such as 'if' in the following patch to introduce conditionals, or 'default' for default values etc.
Signed-off-by Marc-André Lureau <[email protected]>--- scripts/qapi/common.py | 71 ++++++++++++++----- tests/Makefile.include | 6 ++ tests/qapi-schema/alternate-invalid-dict.err | 1 + tests/qapi-schema/alternate-invalid-dict.exit | 1 + tests/qapi-schema/alternate-invalid-dict.json | 4 ++ tests/qapi-schema/alternate-invalid-dict.out | 0 .../qapi-schema/event-member-invalid-dict.err | 1 + .../event-member-invalid-dict.exit | 1 + .../event-member-invalid-dict.json | 2 + .../qapi-schema/event-member-invalid-dict.out | 0 tests/qapi-schema/event-nest-struct.json | 2 +- .../flat-union-inline-invalid-dict.err | 1 + .../flat-union-inline-invalid-dict.exit | 1 + .../flat-union-inline-invalid-dict.json | 11 +++ .../flat-union-inline-invalid-dict.out | 0 tests/qapi-schema/flat-union-inline.json | 2 +- .../nested-struct-data-invalid-dict.err | 1 + .../nested-struct-data-invalid-dict.exit | 1 + .../nested-struct-data-invalid-dict.json | 3 + .../nested-struct-data-invalid-dict.out | 0 tests/qapi-schema/nested-struct-data.json | 2 +- tests/qapi-schema/qapi-schema-test.json | 10 +-- .../struct-member-invalid-dict.err | 1 + .../struct-member-invalid-dict.exit | 1 + .../struct-member-invalid-dict.json | 3 + .../struct-member-invalid-dict.out | 0 .../qapi-schema/union-branch-invalid-dict.err | 1 + .../union-branch-invalid-dict.exit | 1 + .../union-branch-invalid-dict.json | 4 ++ .../qapi-schema/union-branch-invalid-dict.out | 0 30 files changed, 106 insertions(+), 26 deletions(-) create mode 100644 tests/qapi-schema/alternate-invalid-dict.err create mode 100644 tests/qapi-schema/alternate-invalid-dict.exit create mode 100644 tests/qapi-schema/alternate-invalid-dict.json create mode 100644 tests/qapi-schema/alternate-invalid-dict.out create mode 100644 tests/qapi-schema/event-member-invalid-dict.err create mode 100644 tests/qapi-schema/event-member-invalid-dict.exit create mode 100644 tests/qapi-schema/event-member-invalid-dict.json create mode 100644 tests/qapi-schema/event-member-invalid-dict.out create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.err create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.exit create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.json create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.out create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.err create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.exit create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.json create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.out create mode 100644 tests/qapi-schema/struct-member-invalid-dict.err create mode 100644 tests/qapi-schema/struct-member-invalid-dict.exit create mode 100644 tests/qapi-schema/struct-member-invalid-dict.json create mode 100644 tests/qapi-schema/struct-member-invalid-dict.out create mode 100644 tests/qapi-schema/union-branch-invalid-dict.err create mode 100644 tests/qapi-schema/union-branch-invalid-dict.exit create mode 100644 tests/qapi-schema/union-branch-invalid-dict.json create mode 100644 tests/qapi-schema/union-branch-invalid-dict.outscripts/qapi/common.py +0
-0@@ -588,11 +588,13 @@ def discriminator_find_enum_define(expr): if not base_members: return Nonescripts/qapi/common.py def discriminator_find_enum_define(expr): · line 597
Quoted 10 lines
scripts/qapi/common.py +5
-3- discriminator_type = base_members.get(discriminator) - if not discriminator_type: + discriminator_value = base_members.get(discriminator) + if not discriminator_value: return None - return enum_types.get(discriminator_type) + if isinstance(discriminator_value, dict): + discriminator_value = discriminator_value['type'] + return enum_types.get(discriminator_value)As in PATCH 08, this is slightly more complicated than v6 because @expr isn't normalized. I can't say offhand whether normalizing in the place I suggested in my review of v6 would avoid this complication.
Quoted 26 lines
scripts/qapi/common.py +10
-2# Names must be letters, numbers, -, and _. They must start with letter,@@ -660,6 +662,15 @@ def check_if(expr, info): check_if_str(ifcond, info) +def normalize_members(expr, field): + members = expr.get(field) + if isinstance(members, OrderedDict): + for key, arg in members.items(): + if isinstance(arg, dict): + continue + members[key] = {'type': arg} + + def check_type(info, source, value, allow_array=False, allow_implicit=False, allow_optional=False, allow_metas=[]):@@ -704,8 +715,14 @@ def check_type(info, source, value, allow_array=False, % (source, key)) # Todo: allow dictionaries to represent default values of # an optional argument. - check_type(info, "Member '%s' of %s" % (key, source), arg, - allow_array=True, + if isinstance(arg, dict):scripts/qapi/common.py def check_type(info, source, value, allow_array=False, · line 728
Quoted 10 lines
scripts/qapi/common.py +7
-0+ check_known_keys(info, "member '%s' of %s" % (key, source), + arg, ['type'], []) + typ = arg['type'] + else: + typ = arg + check_type(info, "Member '%s' of %s" % (key, source), + typ, allow_array=True, allow_metas=['built-in', 'union', 'alternate', 'struct', 'enum'])Slightly more complicated than v6, this time because members of the implicit struct type aren't normalized, yet.
Quoted 20 lines
scripts/qapi/common.py +5
-3@@ -776,13 +793,15 @@ def check_union(expr, info): # member of the base struct. check_name(info, "Discriminator of flat union '%s'" % name, discriminator) - discriminator_type = base_members.get(discriminator) - if not discriminator_type: + discriminator_value = base_members.get(discriminator) + if not discriminator_value: raise QAPISemError(info, "Discriminator '%s' is not a member of base " "struct '%s'" % (discriminator, base)) - enum_define = enum_types.get(discriminator_type) + if isinstance(discriminator_value, dict): + discriminator_value = discriminator_value['type'] + enum_define = enum_types.get(discriminator_value) allow_metas = ['struct'] # Do not allow string discriminator if not enum_define:scripts/qapi/common.py def check_union(expr, info): · line 822
Quoted 9 lines
scripts/qapi/common.py +6
-0@@ -795,10 +814,16 @@ def check_union(expr, info): raise QAPISemError(info, "Union '%s' cannot have empty 'data'" % name) for (key, value) in members.items(): check_name(info, "Member of union '%s'" % name, key) + if isinstance(value, dict): + check_known_keys(info, "member '%s' of union '%s'" % (key, name), + value, ['type'], []) + typ = value['type'] + else: + typ = valueLikewise.
Quoted 9 lines
scripts/qapi/common.py +1
-1# Each value must name a known type check_type(info, "Member '%s' of union '%s'" % (key, name), - value, allow_array=not base, allow_metas=allow_metas) + typ, allow_array=not base, allow_metas=allow_metas) # If the discriminator names an enum type, then all members # of 'data' must also be members of the enum type.scripts/qapi/common.py def check_alternate(expr, info): · line 856
Quoted 10 lines
scripts/qapi/common.py +7
-0@@ -822,18 +847,24 @@ def check_alternate(expr, info): "in 'data'" % name) for (key, value) in members.items(): check_name(info, "Member of alternate '%s'" % name, key) + if isinstance(value, dict): + check_known_keys(info, + "member '%s' of alternate '%s'" % (key, name), + value, ['type'], []) + typ = value['type'] + else: + typ = valueLikewise.
Quoted 351 lines
scripts/qapi/common.py +12
-9# Ensure alternates have no type conflicts. - check_type(info, "Member '%s' of alternate '%s'" % (key, name), - value, + check_type(info, "Member '%s' of alternate '%s'" % (key, name), typ, allow_metas=['built-in', 'union', 'struct', 'enum']) - qtype = find_alternate_member_qtype(value) + qtype = find_alternate_member_qtype(typ) if not qtype: raise QAPISemError(info, "Alternate '%s' member '%s' cannot use " - "type '%s'" % (name, key, value)) + "type '%s'" % (name, key, typ)) conflicting = set([qtype]) if qtype == 'QTYPE_QSTRING': - enum_expr = enum_types.get(value) + enum_expr = enum_types.get(typ) if enum_expr: for v in enum_get_names(enum_expr): if v in ['on', 'off']:@@ -1035,6 +1066,10 @@ def normalize_exprs(exprs): expr = expr_elem['expr'] if 'enum' in expr: normalize_enum(expr) + elif 'union' in expr: + normalize_members(expr, 'base') + if {'union', 'alternate', 'struct', 'command', 'event'} & set(expr): + normalize_members(expr, 'data') return exprs@@ -1738,7 +1773,7 @@ class QAPISchema(object): return QAPISchemaObjectTypeMember(name, typ, optional) def _make_members(self, data, info): - return [self._make_member(key, value, info) + return [self._make_member(key, value['type'], info) for (key, value) in data.items()] def _def_struct_type(self, expr, info, doc):@@ -1774,11 +1809,11 @@ class QAPISchema(object): name, info, doc, ifcond, 'base', self._make_members(base, info)) if tag_name: - variants = [self._make_variant(key, value) + variants = [self._make_variant(key, value['type']) for (key, value) in data.items()] members = [] else: - variants = [self._make_simple_variant(key, value, info) + variants = [self._make_simple_variant(key, value['type'], info) for (key, value) in data.items()] typ = self._make_implicit_enum_type(name, info, ifcond, [v.name for v in variants])@@ -1794,7 +1829,7 @@ class QAPISchema(object): name = expr['alternate'] data = expr['data'] ifcond = expr.get('if') - variants = [self._make_variant(key, value) + variants = [self._make_variant(key, value['type']) for (key, value) in data.items()] tag_member = QAPISchemaObjectTypeMember('type', 'QType', False) self._def_entity(tests/Makefile.include +6
-0@@ -318,6 +318,7 @@ qapi-schema += alternate-conflict-string.json qapi-schema += alternate-conflict-bool-string.json qapi-schema += alternate-conflict-num-string.json qapi-schema += alternate-empty.json +qapi-schema += alternate-invalid-dict.json qapi-schema += alternate-nested.json qapi-schema += alternate-unknown.json qapi-schema += args-alternate.json@@ -394,6 +395,7 @@ qapi-schema += escape-too-big.json qapi-schema += escape-too-short.json qapi-schema += event-boxed-empty.json qapi-schema += event-case.json +qapi-schema += event-member-invalid-dict.json qapi-schema += event-nest-struct.json qapi-schema += flat-union-array-branch.json qapi-schema += flat-union-bad-base.json@@ -403,6 +405,7 @@ qapi-schema += flat-union-base-union.json qapi-schema += flat-union-clash-member.json qapi-schema += flat-union-empty.json qapi-schema += flat-union-inline.json +qapi-schema += flat-union-inline-invalid-dict.json qapi-schema += flat-union-int-branch.json qapi-schema += flat-union-invalid-branch-key.json qapi-schema += flat-union-invalid-discriminator.json@@ -430,6 +433,7 @@ qapi-schema += missing-comma-list.json qapi-schema += missing-comma-object.json qapi-schema += missing-type.json qapi-schema += nested-struct-data.json +qapi-schema += nested-struct-data-invalid-dict.json qapi-schema += non-objects.json qapi-schema += oob-test.json qapi-schema += allow-preconfig-test.json@@ -460,6 +464,7 @@ 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-member-invalid-dict.json qapi-schema += struct-member-invalid.json qapi-schema += trailing-comma-list.json qapi-schema += trailing-comma-object.json@@ -471,6 +476,7 @@ qapi-schema += unicode-str.json qapi-schema += union-base-empty.json qapi-schema += union-base-no-discriminator.json qapi-schema += union-branch-case.json +qapi-schema += union-branch-invalid-dict.json qapi-schema += union-clash-branches.json qapi-schema += union-empty.json qapi-schema += union-invalid-base.jsontests/qapi-schema/alternate-invalid-dict.err new file +1
-0@@ -0,0 +1 @@ +tests/qapi-schema/alternate-invalid-dict.json:2: Key 'type' is missing from member 'two' of alternate 'Alt'tests/qapi-schema/alternate-invalid-dict.exit new file +1
-0@@ -0,0 +1 @@ +1tests/qapi-schema/alternate-invalid-dict.json new file +4
-0@@ -0,0 +1,4 @@ +# exploded member form must have a 'type' +{ 'alternate': 'Alt', + 'data': { 'one': 'str', + 'two': { 'if': 'foo' } } }tests/qapi-schema/alternate-invalid-dict.out new file +0
-0tests/qapi-schema/event-member-invalid-dict.err new file +1
-0@@ -0,0 +1 @@ +tests/qapi-schema/event-member-invalid-dict.json:1: Key 'type' is missing from member 'a' of 'data' for event 'EVENT_A'tests/qapi-schema/event-member-invalid-dict.exit new file +1
-0@@ -0,0 +1 @@ +1tests/qapi-schema/event-member-invalid-dict.json new file +2
-0@@ -0,0 +1,2 @@ +{ 'event': 'EVENT_A', + 'data': { 'a' : { 'string' : 'str', 'integer': 'int' }, 'b' : 'str' } }tests/qapi-schema/event-member-invalid-dict.out new file +0
-0tests/qapi-schema/event-nest-struct.json +1
-1@@ -1,2 +1,2 @@ { 'event': 'EVENT_A', - 'data': { 'a' : { 'string' : 'str', 'integer': 'int' }, 'b' : 'str' } } + 'data': { 'a' : { 'type' : { 'integer': 'int' } }, 'b' : 'str' } }tests/qapi-schema/flat-union-inline-invalid-dict.err new file +1
-0@@ -0,0 +1 @@ +tests/qapi-schema/flat-union-inline-invalid-dict.json:7: Key 'type' is missing from member 'value1' of union 'TestUnion'tests/qapi-schema/flat-union-inline-invalid-dict.exit new file +1
-0@@ -0,0 +1 @@ +1tests/qapi-schema/flat-union-inline-invalid-dict.json new file +11
-0@@ -0,0 +1,11 @@ +# we require branches to be a struct name +# TODO: should we allow anonymous inline branch types? +{ 'enum': 'TestEnum', + 'data': [ 'value1', 'value2' ] } +{ 'struct': 'Base', + 'data': { 'enum1': 'TestEnum', 'kind': 'str' } } +{ 'union': 'TestUnion', + 'base': 'Base', + 'discriminator': 'enum1', + 'data': { 'value1': { 'string': 'str' }, + 'value2': { 'integer': 'int' } } }tests/qapi-schema/flat-union-inline-invalid-dict.out new file +0
-0tests/qapi-schema/flat-union-inline.json +1
-1@@ -7,5 +7,5 @@ { 'union': 'TestUnion', 'base': 'Base', 'discriminator': 'enum1', - 'data': { 'value1': { 'string': 'str' }, + 'data': { 'value1': { 'type': {} }, 'value2': { 'integer': 'int' } } }tests/qapi-schema/nested-struct-data-invalid-dict.err new file +1
-0@@ -0,0 +1 @@ +tests/qapi-schema/nested-struct-data-invalid-dict.json:2: Key 'type' is missing from member 'a' of 'data' for command 'foo'tests/qapi-schema/nested-struct-data-invalid-dict.exit new file +1
-0@@ -0,0 +1 @@ +1tests/qapi-schema/nested-struct-data-invalid-dict.json new file +3
-0@@ -0,0 +1,3 @@ +# inline subtypes collide with our desired future use of defaults +{ 'command': 'foo', + 'data': { 'a' : { 'string' : 'str', 'integer': 'int' }, 'b' : 'str' } }tests/qapi-schema/nested-struct-data-invalid-dict.out new file +0
-0tests/qapi-schema/nested-struct-data.json +1
-1@@ -1,3 +1,3 @@ # inline subtypes collide with our desired future use of defaults { 'command': 'foo', - 'data': { 'a' : { 'string' : 'str', 'integer': 'int' }, 'b' : 'str' } } + 'data': { 'a' : { 'type': {} }, 'b' : 'str' } }tests/qapi-schema/qapi-schema-test.json +5
-5@@ -11,7 +11,7 @@ 'guest-sync' ] } } { 'struct': 'TestStruct', - 'data': { 'integer': 'int', 'boolean': 'bool', 'string': 'str' } } + 'data': { 'integer': {'type': 'int'}, 'boolean': 'bool', 'string': 'str' } } # for testing enums { 'struct': 'NestedEnumsOne',@@ -77,7 +77,7 @@ { 'union': 'UserDefFlatUnion', 'base': 'UserDefUnionBase', # intentional forward reference 'discriminator': 'enum1', - 'data': { 'value1' : 'UserDefA', + 'data': { 'value1' : {'type': 'UserDefA'}, 'value2' : 'UserDefB', 'value3' : 'UserDefB' # 'value4' defaults to empty@@ -98,7 +98,7 @@ { 'struct': 'WrapAlternate', 'data': { 'alt': 'UserDefAlternate' } } { 'alternate': 'UserDefAlternate', - 'data': { 'udfu': 'UserDefFlatUnion', 'e': 'EnumOne', 'i': 'int', + 'data': { 'udfu': {'type': 'UserDefFlatUnion'}, 'e': 'EnumOne', 'i': 'int', 'n': 'null' } } { 'struct': 'UserDefC',@@ -134,7 +134,7 @@ { 'command': 'user_def_cmd', 'data': {} } { 'command': 'user_def_cmd1', 'data': {'ud1a': 'UserDefOne'} } { 'command': 'user_def_cmd2', - 'data': {'ud1a': 'UserDefOne', '*ud1b': 'UserDefOne'}, + 'data': {'ud1a': {'type': 'UserDefOne'}, '*ud1b': 'UserDefOne'}, 'returns': 'UserDefTwo' } { 'command': 'cmd-success-response', 'data': {}, 'success-response': false }@@ -166,7 +166,7 @@ # testing event { 'struct': 'EventStructOne', - 'data': { 'struct1': 'UserDefOne', 'string': 'str', '*enum2': 'EnumOne' } } + 'data': { 'struct1': {'type': 'UserDefOne'}, 'string': 'str', '*enum2': 'EnumOne' } } { 'event': 'EVENT_A' } { 'event': 'EVENT_B',tests/qapi-schema/struct-member-invalid-dict.err new file +1
-0@@ -0,0 +1 @@ +tests/qapi-schema/struct-member-invalid-dict.json:2: Key 'type' is missing from member '*a' of 'data' for struct 'foo'tests/qapi-schema/struct-member-invalid-dict.exit new file +1
-0@@ -0,0 +1 @@ +1tests/qapi-schema/struct-member-invalid-dict.json new file +3
-0@@ -0,0 +1,3 @@ +# Long form of member must have a value member 'type' +{ 'struct': 'foo', + 'data': { '*a': { 'case': 'foo' } } }tests/qapi-schema/struct-member-invalid-dict.out new file +0
-0tests/qapi-schema/union-branch-invalid-dict.err new file +1
-0@@ -0,0 +1 @@ +tests/qapi-schema/union-branch-invalid-dict.json:2: Key 'type' is missing from member 'integer' of union 'UnionInvalidBranch'tests/qapi-schema/union-branch-invalid-dict.exit new file +1
-0@@ -0,0 +1 @@ +1tests/qapi-schema/union-branch-invalid-dict.json new file +2
-0@@ -0,0 +1,4 @@ +# Long form of member must have a value member 'type' +{ 'union': 'UnionInvalidBranch',
Quoted 9 lines
tests/qapi-schema/union-branch-invalid-dict.out
Quoted 5 lines
tests/qapi-schema/union-branch-invalid-dict.json new file +2
-0+ 'data': { 'integer': { 'if': 'foo'}, + 's8': 'int8' } }tests/qapi-schema/union-branch-invalid-dict.out new file +0
-0Looks good, but I'd like to investigate whether my suggested placement of the normalization would simplify things before I give my R-by.
I am not sure I follow, you suggest to have normalization before check? Then how can we produce an error that reflects the input accurately? Should we carry additional information (original form etc) on the source during normalization?
Signature · 1 line
Marc-André Lureau
Re: [Qemu-devel] [PATCH for-4.0 v7 13/27] qapi: add a dictionary form for TYPE
Quoted 3 lines · Marc-André Lureau
Hi
On Mon, Dec 10, 2018 at 9:26 PM Markus Armbruster <[email protected]> wrote:
[...]
Quoted 8 lines
Quoted 3 lines
Looks good, but I'd like to investigate whether my suggested placement of the normalization would simplify things before I give my R-by.
I am not sure I follow, you suggest to have normalization before check? Then how can we produce an error that reflects the input accurately? Should we carry additional information (original form etc) on the source during normalization?
Let me play with it. If my idea works out, showing it to you will be easier than explaining it. If it doesn't, ditching it quietly will also be easier than explaining it :)
Re: [Qemu-devel] [PATCH for-4.0 v7 13/27] qapi: add a dictionary form for TYPE
In reply to Markus ArmbrusterNew pylint gripes:
scripts/qapi/common.py:674:0: R0912: Too many branches (13/12) (too-many-branches) scripts/qapi/common.py:764:0: R0912: Too many branches (13/12) (too-many-branches) scripts/qapi/common.py:838:0: R0912: Too many branches (13/12) (too-many-branches)
That's check_type(), check_union(), check_alternate(). Let's ignore these for now.
PATCH v7 14/27 qapi: add 'if' to implicit struct members
In reply to Marc-André LureauThe generated code is for now *unconditional*. Later patches generate the conditionals.
--- scripts/qapi/common.py | 14 +++++++------- docs/devel/qapi-code-gen.txt | 10 ++++++++++ tests/qapi-schema/qapi-schema-test.json | 12 +++++++++--- tests/qapi-schema/qapi-schema-test.out | 5 +++++ tests/qapi-schema/test-qapi.py | 1 + 5 files changed, 32 insertions(+), 10 deletions(-)
scripts/qapi/common.py +7 -7
@@ -717,7 +717,7 @@ def check_type(info, source, value, allow_array=False, # an optional argument. if isinstance(arg, dict): check_known_keys(info, "member '%s' of %s" % (key, source), - arg, ['type'], []) + arg, ['type'], ['if']) typ = arg['type'] else: typ = arg
@@ -1438,8 +1438,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
@@ -1762,7 +1762,7 @@ class QAPISchema(object): name, info, doc, ifcond, self._make_enum_members(data), prefix)) - def _make_member(self, name, typ, info): + def _make_member(self, name, typ, ifcond, info): optional = False if name.startswith('*'): name = name[1:]
@@ -1770,10 +1770,10 @@ class QAPISchema(object): 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['type'], info) + return [self._make_member(key, value['type'], value.get('if'), info) for (key, value) in data.items()] def _def_struct_type(self, expr, info, doc):
@@ -1794,7 +1794,7 @@ class QAPISchema(object): typ = self._make_array_type(typ[0], info) typ = self._make_implicit_object_type( typ, info, None, self.lookup_type(typ), - 'wrapper', [self._make_member('data', typ, info)]) + 'wrapper', [self._make_member('data', typ, None, info)]) return QAPISchemaObjectTypeVariant(case, typ) def _def_union_type(self, expr, info, doc):
docs/devel/qapi-code-gen.txt +10 -0
@@ -752,6 +752,16 @@ gets its generated code guarded like this: #endif /* defined(HAVE_BAR) */ #endif /* defined(CONFIG_FOO) */ +Where a member can be defined with a single string value for its type, +it is also possible to supply a dictionary instead with both 'type' +and 'if' keys. (TODO: union and alternate) + +Example: a conditional 'bar' member + +{ 'struct': 'IfStruct', 'data': + { 'foo': 'int', + 'bar': { 'type': 'int', 'if': 'defined(IFCOND)'} } } + An enum value can be replaced by a dictionary with a 'name' and a 'if' key.
tests/qapi-schema/qapi-schema-test.json +9 -3
@@ -201,7 +201,9 @@ # 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':
@@ -214,11 +216,15 @@ { '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': 'defined(TEST_IF_CMD_BAR)' } }, 'returns': 'UserDefThree', 'if': ['defined(TEST_IF_CMD)', 'defined(TEST_IF_STRUCT)'] } { 'command': 'TestCmdReturnDefThree', 'returns': 'UserDefThree' } -{ 'event': 'TestIfEvent', 'data': { 'foo': 'TestIfStruct' }, +{ 'event': 'TestIfEvent', 'data': + { 'foo': 'TestIfStruct', + 'bar': { 'type': 'TestIfEnum', 'if': 'defined(TEST_IF_EVT_BAR)' } }, 'if': 'defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)' }
tests/qapi-schema/qapi-schema-test.out +5 -0
@@ -268,6 +268,8 @@ command __org.qemu_x-command q_obj___org.qemu_x-command-arg -> __org.qemu_x-Unio gen=True success_response=True boxed=False oob=False preconfig=False object TestIfStruct member foo: int optional=False + member bar: int optional=False + if ['defined(TEST_IF_STRUCT_BAR)'] if ['defined(TEST_IF_STRUCT)'] enum TestIfEnum member foo
@@ -292,6 +294,7 @@ alternate TestIfAlternate object q_obj_TestIfCmd-arg member foo: TestIfStruct optional=False member bar: TestIfEnum optional=False + if ['defined(TEST_IF_CMD_BAR)'] if ['defined(TEST_IF_CMD)', 'defined(TEST_IF_STRUCT)'] command TestIfCmd q_obj_TestIfCmd-arg -> UserDefThree gen=True success_response=True boxed=False oob=False preconfig=False
@@ -300,6 +303,8 @@ command TestCmdReturnDefThree None -> UserDefThree gen=True success_response=True boxed=False oob=False preconfig=False object q_obj_TestIfEvent-arg member foo: TestIfStruct optional=False + member bar: TestIfEnum optional=False + if ['defined(TEST_IF_EVT_BAR)'] if ['defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)'] event TestIfEvent q_obj_TestIfEvent-arg boxed=False
tests/qapi-schema/test-qapi.py +1 -0
@@ -39,6 +39,7 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor): for m in members: print(' member %s: %s optional=%s' % (m.name, m.type.name, m.optional)) + self._print_if(m.ifcond, 8) self._print_variants(variants) self._print_if(ifcond)
Signature · 1 line
2.20.0.rc1
PATCH v7 15/27 qapi: add an error in case a discriminator is conditional
In reply to Marc-André LureauMaking a discriminator conditional doesn't make much sense. Instead, the union could be made conditional.
--- scripts/qapi/common.py | 8 ++++++++ tests/Makefile.include | 1 + .../flat-union-invalid-if-discriminator.err | 1 + .../flat-union-invalid-if-discriminator.exit | 1 + .../flat-union-invalid-if-discriminator.json | 17 +++++++++++++++++ .../flat-union-invalid-if-discriminator.out | 0 6 files changed, 28 insertions(+) create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.err create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.exit create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.json create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.out
scripts/qapi/common.py +8 -0
@@ -578,6 +578,7 @@ def find_alternate_member_qtype(qapi_type): # Return the discriminator enum define if discriminator is specified as an # enum type, otherwise return None. def discriminator_find_enum_define(expr): + name = expr['union'] base = expr.get('base') discriminator = expr.get('discriminator')
@@ -594,6 +595,7 @@ def discriminator_find_enum_define(expr): if isinstance(discriminator_value, dict): discriminator_value = discriminator_value['type'] + return enum_types.get(discriminator_value)
@@ -800,7 +802,12 @@ def check_union(expr, info): "struct '%s'" % (discriminator, base)) if isinstance(discriminator_value, dict): + if discriminator_value.get('if'): + raise QAPISemError(info, 'The discriminator %s.%s for union %s ' + 'must not be conditional' % + (base, discriminator, name)) discriminator_value = discriminator_value['type'] + enum_define = enum_types.get(discriminator_value) allow_metas = ['struct'] # Do not allow string discriminator
@@ -1023,6 +1030,7 @@ def check_exprs(exprs): if 'include' in expr: continue + info = expr_elem['info'] if 'union' in expr and not discriminator_find_enum_define(expr): name = '%sKind' % expr['union'] elif 'alternate' in expr:
tests/Makefile.include +1 -0
@@ -409,6 +409,7 @@ qapi-schema += flat-union-inline-invalid-dict.json qapi-schema += flat-union-int-branch.json qapi-schema += flat-union-invalid-branch-key.json qapi-schema += flat-union-invalid-discriminator.json +qapi-schema += flat-union-invalid-if-discriminator.json qapi-schema += flat-union-no-base.json qapi-schema += flat-union-optional-discriminator.json qapi-schema += flat-union-string-discriminator.json
tests/qapi-schema/flat-union-invalid-if-discriminator.err new file +1 -0
@@ -0,0 +1 @@ +tests/qapi-schema/flat-union-invalid-if-discriminator.json:13: The discriminator TestBase.enum1 for union TestUnion must not be conditional
tests/qapi-schema/flat-union-invalid-if-discriminator.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/flat-union-invalid-if-discriminator.json new file +17 -0
@@ -0,0 +1,17 @@ +{ 'enum': 'TestEnum', + 'data': [ 'value1', 'value2' ] } + +{ 'struct': 'TestBase', + 'data': { 'enum1': { 'type': 'TestEnum', 'if': 'FOO' } } } + +{ 'struct': 'TestTypeA', + 'data': { 'string': 'str' } } + +{ 'struct': 'TestTypeB', + 'data': { 'integer': 'int' } } + +{ 'union': 'TestUnion', + 'base': 'TestBase', + 'discriminator': 'enum1', + 'data': { 'value1': 'TestTypeA', + 'value2': 'TestTypeB' } }
tests/qapi-schema/flat-union-invalid-if-discriminator.out new file +0 -0
Signature · 1 line
2.20.0.rc1
Re: [Qemu-devel] [PATCH for-4.0 v7 15/27] qapi: add an error in case a discriminator is conditional
Quoted 1 line · Marc-André Lureau
Making a discriminator conditional doesn't make much sense.
Good point (so easy to overlook!), but why first add the 'if' feature broken that way in PATCH 13, then fix it up in PATCH 15?
Quoted 2 lines
Instead,
the union could be made conditional.
What do you mean by that?
Quoted 27 lines
Signed-off-by Marc-André Lureau <[email protected]>--- scripts/qapi/common.py | 8 ++++++++ tests/Makefile.include | 1 + .../flat-union-invalid-if-discriminator.err | 1 + .../flat-union-invalid-if-discriminator.exit | 1 + .../flat-union-invalid-if-discriminator.json | 17 +++++++++++++++++ .../flat-union-invalid-if-discriminator.out | 0 6 files changed, 28 insertions(+) create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.err create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.exit create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.json create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.outscripts/qapi/common.py +1
-0@@ -578,6 +578,7 @@ def find_alternate_member_qtype(qapi_type): # Return the discriminator enum define if discriminator is specified as an # enum type, otherwise return None. def discriminator_find_enum_define(expr): + name = expr['union'] base = expr.get('base') discriminator = expr.get('discriminator')
scripts/qapi/common.py def discriminator_find_enum_define(expr): · line 601
Quoted 7 lines
scripts/qapi/common.py +1
-0@@ -594,6 +595,7 @@ def discriminator_find_enum_define(expr): if isinstance(discriminator_value, dict): discriminator_value = discriminator_value['type'] + return enum_types.get(discriminator_value)
These two hunks are leftovers, please drop.
tests/qapi-schema/flat-union-invalid-if-discriminator.out
Quoted 73 lines
scripts/qapi/common.py +6
-0@@ -800,7 +802,12 @@ def check_union(expr, info): "struct '%s'" % (discriminator, base)) if isinstance(discriminator_value, dict): + if discriminator_value.get('if'): + raise QAPISemError(info, 'The discriminator %s.%s for union %s ' + 'must not be conditional' % + (base, discriminator, name)) discriminator_value = discriminator_value['type'] + enum_define = enum_types.get(discriminator_value) allow_metas = ['struct'] # Do not allow string discriminator@@ -1023,6 +1030,7 @@ def check_exprs(exprs): if 'include' in expr: continue + info = expr_elem['info'] if 'union' in expr and not discriminator_find_enum_define(expr): name = '%sKind' % expr['union'] elif 'alternate' in expr:tests/Makefile.include +1
-0@@ -409,6 +409,7 @@ qapi-schema += flat-union-inline-invalid-dict.json qapi-schema += flat-union-int-branch.json qapi-schema += flat-union-invalid-branch-key.json qapi-schema += flat-union-invalid-discriminator.json +qapi-schema += flat-union-invalid-if-discriminator.json qapi-schema += flat-union-no-base.json qapi-schema += flat-union-optional-discriminator.json qapi-schema += flat-union-string-discriminator.jsontests/qapi-schema/flat-union-invalid-if-discriminator.err new file +1
-0@@ -0,0 +1 @@ +tests/qapi-schema/flat-union-invalid-if-discriminator.json:13: The discriminator TestBase.enum1 for union TestUnion must not be conditionaltests/qapi-schema/flat-union-invalid-if-discriminator.exit new file +1
-0@@ -0,0 +1 @@ +1tests/qapi-schema/flat-union-invalid-if-discriminator.json new file +17
-0@@ -0,0 +1,17 @@ +{ 'enum': 'TestEnum', + 'data': [ 'value1', 'value2' ] } + +{ 'struct': 'TestBase', + 'data': { 'enum1': { 'type': 'TestEnum', 'if': 'FOO' } } } + +{ 'struct': 'TestTypeA', + 'data': { 'string': 'str' } } + +{ 'struct': 'TestTypeB', + 'data': { 'integer': 'int' } } + +{ 'union': 'TestUnion', + 'base': 'TestBase', + 'discriminator': 'enum1', + 'data': { 'value1': 'TestTypeA', + 'value2': 'TestTypeB' } }tests/qapi-schema/flat-union-invalid-if-discriminator.out new file +0
-0
Patch looks good otherwise.
Re: [Qemu-devel] [PATCH for-4.0 v7 15/27] qapi: add an error in case a discriminator is conditional
Two more nits...
Quoted 26 lines · Markus Armbruster
Quoted 1 line · Marc-André Lureau
Making a discriminator conditional doesn't make much sense.
Good point (so easy to overlook!), but why first add the 'if' feature broken that way in PATCH 13, then fix it up in PATCH 15?
Quoted 2 lines
Instead,
the union could be made conditional.What do you mean by that?
Quoted 14 lines
Signed-off-by Marc-André Lureau <[email protected]>--- scripts/qapi/common.py | 8 ++++++++ tests/Makefile.include | 1 + .../flat-union-invalid-if-discriminator.err | 1 + .../flat-union-invalid-if-discriminator.exit | 1 + .../flat-union-invalid-if-discriminator.json | 17 +++++++++++++++++ .../flat-union-invalid-if-discriminator.out | 0 6 files changed, 28 insertions(+) create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.err create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.exit create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.json create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.out
Quoted 9 lines
scripts/qapi/common.py def find_alternate_member_qtype(qapi_type): · line 581
Quoted 9 lines
scripts/qapi/common.py +1
-0@@ -578,6 +578,7 @@ def find_alternate_member_qtype(qapi_type): # Return the discriminator enum define if discriminator is specified as an # enum type, otherwise return None. def discriminator_find_enum_define(expr): + name = expr['union']
Dead assignment.
Quoted 14 lines
scripts/qapi/common.py def discriminator_find_enum_define(expr): · line 601
Quoted 11 lines
scripts/qapi/common.py +1
-0base = expr.get('base') discriminator = expr.get('discriminator')@@ -594,6 +595,7 @@ def discriminator_find_enum_define(expr): if isinstance(discriminator_value, dict): discriminator_value = discriminator_value['type'] + return enum_types.get(discriminator_value)These two hunks are leftovers, please drop.
Quoted 6 lines
scripts/qapi/common.py def check_union(expr, info): · line 806
Quoted 6 lines
scripts/qapi/common.py +2
-0@@ -800,7 +802,12 @@ def check_union(expr, info): "struct '%s'" % (discriminator, base)) if isinstance(discriminator_value, dict): + if discriminator_value.get('if'): + raise QAPISemError(info, 'The discriminator %s.%s for union %s '
pycodestyle complains
scripts/qapi/common.py:806:80: E501 line too long (80 > 79 characters)
Quoted 69 lines
tests/qapi-schema/flat-union-invalid-if-discriminator.out
Quoted 67 lines
scripts/qapi/common.py +4
-0+ 'must not be conditional' % + (base, discriminator, name)) discriminator_value = discriminator_value['type'] + enum_define = enum_types.get(discriminator_value) allow_metas = ['struct'] # Do not allow string discriminator@@ -1023,6 +1030,7 @@ def check_exprs(exprs): if 'include' in expr: continue + info = expr_elem['info'] if 'union' in expr and not discriminator_find_enum_define(expr): name = '%sKind' % expr['union'] elif 'alternate' in expr:tests/Makefile.include +1
-0@@ -409,6 +409,7 @@ qapi-schema += flat-union-inline-invalid-dict.json qapi-schema += flat-union-int-branch.json qapi-schema += flat-union-invalid-branch-key.json qapi-schema += flat-union-invalid-discriminator.json +qapi-schema += flat-union-invalid-if-discriminator.json qapi-schema += flat-union-no-base.json qapi-schema += flat-union-optional-discriminator.json qapi-schema += flat-union-string-discriminator.jsontests/qapi-schema/flat-union-invalid-if-discriminator.err new file +1
-0@@ -0,0 +1 @@ +tests/qapi-schema/flat-union-invalid-if-discriminator.json:13: The discriminator TestBase.enum1 for union TestUnion must not be conditionaltests/qapi-schema/flat-union-invalid-if-discriminator.exit new file +1
-0@@ -0,0 +1 @@ +1tests/qapi-schema/flat-union-invalid-if-discriminator.json new file +17
-0@@ -0,0 +1,17 @@ +{ 'enum': 'TestEnum', + 'data': [ 'value1', 'value2' ] } + +{ 'struct': 'TestBase', + 'data': { 'enum1': { 'type': 'TestEnum', 'if': 'FOO' } } } + +{ 'struct': 'TestTypeA', + 'data': { 'string': 'str' } } + +{ 'struct': 'TestTypeB', + 'data': { 'integer': 'int' } } + +{ 'union': 'TestUnion', + 'base': 'TestBase', + 'discriminator': 'enum1', + 'data': { 'value1': 'TestTypeA', + 'value2': 'TestTypeB' } }tests/qapi-schema/flat-union-invalid-if-discriminator.out new file +0
-0Patch looks good otherwise.
PATCH v7 16/27 qapi: add 'if' to union members
In reply to Marc-André LureauAdd 'if' key to union members:
{ 'union': 'TestIfUnion', 'data':
'mem': { 'type': 'str', 'if': 'COND'} }The generated code remains unconditional for now. Later patches generate the conditionals.
--- scripts/qapi/common.py | 17 +++++++++-------- docs/devel/qapi-code-gen.txt | 2 +- tests/qapi-schema/qapi-schema-test.json | 4 +++- tests/qapi-schema/qapi-schema-test.out | 4 ++++ tests/qapi-schema/test-qapi.py | 1 + 5 files changed, 18 insertions(+), 10 deletions(-)
scripts/qapi/common.py +9 -8
@@ -823,7 +823,7 @@ def check_union(expr, info): check_name(info, "Member of union '%s'" % name, key) if isinstance(value, dict): check_known_keys(info, "member '%s' of union '%s'" % (key, name), - value, ['type'], []) + value, ['type'], ['if']) typ = value['type'] else: typ = value
@@ -1513,8 +1513,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):
@@ -1796,14 +1796,14 @@ class QAPISchema(object): def _make_variant(self, case, typ): return QAPISchemaObjectTypeVariant(case, typ) - def _make_simple_variant(self, case, typ, info): + def _make_simple_variant(self, case, typ, ifcond, info): if isinstance(typ, list): assert len(typ) == 1 typ = self._make_array_type(typ[0], info) typ = self._make_implicit_object_type( typ, info, None, self.lookup_type(typ), 'wrapper', [self._make_member('data', typ, None, info)]) - return QAPISchemaObjectTypeVariant(case, typ) + return QAPISchemaObjectTypeVariant(case, typ, ifcond) def _def_union_type(self, expr, info, doc): name = expr['union']
@@ -1821,10 +1821,11 @@ class QAPISchema(object): for (key, value) in data.items()] members = [] else: - variants = [self._make_simple_variant(key, value['type'], info) + variants = [self._make_simple_variant(key, value['type'], + value.get('if'), info) for (key, value) in data.items()] - typ = self._make_implicit_enum_type(name, info, ifcond, - [v.name for v in variants]) + enum = [{'name': v.name, 'if': v.ifcond} for v in variants] + typ = self._make_implicit_enum_type(name, info, ifcond, enum) tag_member = QAPISchemaObjectTypeMember('type', typ, False) members = [tag_member] self._def_entity(
docs/devel/qapi-code-gen.txt +1 -1
@@ -754,7 +754,7 @@ gets its generated code guarded like this: Where a member can be defined with a single string value for its type, it is also possible to supply a dictionary instead with both 'type' -and 'if' keys. (TODO: union and alternate) +and 'if' keys. (TODO: alternate) Example: a conditional 'bar' member
tests/qapi-schema/qapi-schema-test.json +3 -1
@@ -210,7 +210,9 @@ [ '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)' } { 'alternate': 'TestIfAlternate', 'data': { 'foo': 'int', 'bar': 'TestStruct' },
tests/qapi-schema/qapi-schema-test.out +4 -0
@@ -280,11 +280,15 @@ object q_obj_TestStruct-wrapper member data: TestStruct optional=False enum TestIfUnionKind member foo + member union_bar + if ['defined(TEST_IF_UNION_BAR)'] if ['defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)'] 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)'] alternate TestIfAlternate tag type
tests/qapi-schema/test-qapi.py +1 -0
@@ -68,6 +68,7 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor): print(' tag %s' % variants.tag_member.name) for v in variants.variants: print(' case %s: %s' % (v.name, v.type.name)) + QAPISchemaTestVisitor._print_if(v.ifcond, indent=8) @staticmethod def _print_if(ifcond, indent=4):
Signature · 1 line
2.20.0.rc1
Re: [Qemu-devel] [PATCH for-4.0 v7 16/27] qapi: add 'if' to union members
Quoted 9 lines · Marc-André Lureau
Add 'if' key to union members:
{ 'union': 'TestIfUnion', 'data':
'mem': { 'type': 'str', 'if': 'COND'} }The generated code remains unconditional for now. Later patches generate the conditionals.
Signed-off-by Marc-André Lureau <[email protected]>
PATCH v7 17/27 qapi: simplify make_enum_members()
In reply to Marc-André LureauThe function only receives the dictionary form of enum expressions now, so we can make it shorter.
--- scripts/qapi/common.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-)
scripts/qapi/common.py +1 -10
@@ -1714,16 +1714,7 @@ class QAPISchema(object): qtype_values, 'QTYPE')) def _make_enum_members(self, values): - enum = [] - for v in values: - if isinstance(v, dict): - name = v['name'] - ifcond = v.get('if') - else: - name = v - ifcond = None - enum.append(QAPISchemaMember(name, ifcond)) - return enum + return [QAPISchemaMember(v['name'], v.get('if')) for v in values] def _make_implicit_enum_type(self, name, info, ifcond, values): # See also QAPISchemaObjectTypeMember._pretty_owner()
Signature · 1 line
2.20.0.rc1
Re: [Qemu-devel] [PATCH for-4.0 v7 17/27] qapi: simplify make_enum_members()
Quoted 21 lines · Marc-André Lureau
The function only receives the dictionary form of enum expressions now, so we can make it shorter.
Suggested-by Markus Armbruster <[email protected]>Signed-off-by Marc-André Lureau <[email protected]>--- scripts/qapi/common.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-)scripts/qapi/common.py +0
-4@@ -1714,16 +1714,7 @@ class QAPISchema(object): qtype_values, 'QTYPE')) def _make_enum_members(self, values): - enum = [] - for v in values: - if isinstance(v, dict): - name = v['name']
scripts/qapi/common.py class QAPISchema(object): · line 1720
Quoted 10 lines
scripts/qapi/common.py +1
-6- ifcond = v.get('if') - else: - name = v - ifcond = None - enum.append(QAPISchemaMember(name, ifcond)) - return enum + return [QAPISchemaMember(v['name'], v.get('if')) for v in values] def _make_implicit_enum_type(self, name, info, ifcond, values): # See also QAPISchemaObjectTypeMember._pretty_owner()
We may want to squash this one into PATCH 08 as mentioned in that patch's review.
In case we don't:
Re: [Qemu-devel] [PATCH for-4.0 v7 17/27] qapi: simplify make_enum_members()
Hi
Quoted 32 lines · Markus Armbruster
Quoted 29 lines · Marc-André Lureau
The function only receives the dictionary form of enum expressions now, so we can make it shorter.
Suggested-by Markus Armbruster <[email protected]>Signed-off-by Marc-André Lureau <[email protected]>--- scripts/qapi/common.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-)scripts/qapi/common.py +1
-10@@ -1714,16 +1714,7 @@ class QAPISchema(object): qtype_values, 'QTYPE')) def _make_enum_members(self, values): - enum = [] - for v in values: - if isinstance(v, dict): - name = v['name'] - ifcond = v.get('if') - else: - name = v - ifcond = None - enum.append(QAPISchemaMember(name, ifcond)) - return enum + return [QAPISchemaMember(v['name'], v.get('if')) for v in values]
Quoted 9 lines
scripts/qapi/common.py class QAPISchema(object): · line 1720
Quoted 2 lines
scripts/qapi/common.py +0
-0def _make_implicit_enum_type(self, name, info, ifcond, values): # See also QAPISchemaObjectTypeMember._pretty_owner()We may want to squash this one into PATCH 08 as mentioned in that patch's review.
In case we don't:
Reviewed-by Markus Armbruster <[email protected]>
Feel free to squash on commit, thanks
Signature · 1 line
Marc-André Lureau
PATCH v7 18/27 tests/qapi: add command with condition on union argument
In reply to Marc-André LureauFor completeness.
--- tests/qapi-schema/qapi-schema-test.json | 3 +++ tests/qapi-schema/qapi-schema-test.out | 6 ++++++ 2 files changed, 9 insertions(+)
tests/qapi-schema/qapi-schema-test.json +3 -0
@@ -215,6 +215,9 @@ '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 +6 -0
@@ -290,6 +290,12 @@ object TestIfUnion case union_bar: q_obj_str-wrapper if ['defined(TEST_IF_UNION_BAR)'] if ['defined(TEST_IF_UNION) && defined(TEST_IF_STRUCT)'] +object q_obj_TestIfUnionCmd-arg + member union_cmd_arg: TestIfUnion optional=False + if ['defined(TEST_IF_UNION)'] +command TestIfUnionCmd q_obj_TestIfUnionCmd-arg -> None + gen=True success_response=True boxed=False oob=False preconfig=False + if ['defined(TEST_IF_UNION)'] alternate TestIfAlternate tag type case foo: int
Signature · 1 line
2.20.0.rc1
Re: [Qemu-devel] [PATCH for-4.0 v7 18/27] tests/qapi: add command with condition on union argument
Quoted 3 lines · Marc-André Lureau
For completeness.
Signed-off-by Marc-André Lureau <[email protected]>
Let's say "Forgotten in commit 967c885108f."
Re: [Qemu-devel] [PATCH for-4.0 v7 18/27] tests/qapi: add command with condition on union argument
Quoted 10 lines · Markus Armbruster
Quoted 3 lines · Marc-André Lureau
For completeness.
Signed-off-by Marc-André Lureau <[email protected]>Let's say "Forgotten in commit
967c885108f."Reviewed-by Markus Armbruster <[email protected]>
ack, thanks
Signature · 1 line
Marc-André Lureau
PATCH v7 19/27 qapi: add 'if' to alternate members
In reply to Marc-André LureauAdd 'if' key to alternate members:
{ 'alternate': 'TestIfAlternate', 'data':
{ 'alt': { 'type': 'TestStruct', 'if': 'COND' } } }
Generated code is not changed by this patch but with "qapi: add #if conditions to generated code".
--- scripts/qapi/common.py | 10 +++++----- docs/devel/qapi-code-gen.txt | 2 +- tests/qapi-schema/qapi-schema-test.json | 4 +++- tests/qapi-schema/qapi-schema-test.out | 1 + 4 files changed, 10 insertions(+), 7 deletions(-)
scripts/qapi/common.py +5 -5
@@ -857,7 +857,7 @@ def check_alternate(expr, info): if isinstance(value, dict): check_known_keys(info, "member '%s' of alternate '%s'" % (key, name), - value, ['type'], []) + value, ['type'], ['if']) typ = value['type'] else: typ = value
@@ -1784,8 +1784,8 @@ class QAPISchema(object): self._make_members(data, info), None)) - def _make_variant(self, case, typ): - return QAPISchemaObjectTypeVariant(case, typ) + def _make_variant(self, case, typ, ifcond): + return QAPISchemaObjectTypeVariant(case, typ, ifcond) def _make_simple_variant(self, case, typ, ifcond, info): if isinstance(typ, list):
@@ -1808,7 +1808,7 @@ class QAPISchema(object): name, info, doc, ifcond, 'base', self._make_members(base, info)) if tag_name: - variants = [self._make_variant(key, value['type']) + variants = [self._make_variant(key, value['type'], value.get('if')) for (key, value) in data.items()] members = [] else:
@@ -1829,7 +1829,7 @@ class QAPISchema(object): name = expr['alternate'] data = expr['data'] ifcond = expr.get('if') - variants = [self._make_variant(key, value['type']) + variants = [self._make_variant(key, value['type'], value.get('if')) for (key, value) in data.items()] tag_member = QAPISchemaObjectTypeMember('type', 'QType', False) self._def_entity(
docs/devel/qapi-code-gen.txt +1 -1
@@ -754,7 +754,7 @@ gets its generated code guarded like this: Where a member can be defined with a single string value for its type, it is also possible to supply a dictionary instead with both 'type' -and 'if' keys. (TODO: alternate) +and 'if' keys. Example: a conditional 'bar' member
tests/qapi-schema/qapi-schema-test.json +3 -1
@@ -218,7 +218,9 @@ { 'command': 'TestIfUnionCmd', 'data': { 'union_cmd_arg': 'TestIfUnion' }, 'if': 'defined(TEST_IF_UNION)' } -{ 'alternate': 'TestIfAlternate', 'data': { 'foo': 'int', 'bar': 'TestStruct' }, +{ 'alternate': 'TestIfAlternate', 'data': + { 'foo': 'int', + 'bar': { 'type': 'TestStruct', 'if': 'defined(TEST_IF_ALT_BAR)'} }, 'if': 'defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)' } { 'command': 'TestIfCmd', 'data':
tests/qapi-schema/qapi-schema-test.out +1 -0
@@ -300,6 +300,7 @@ alternate TestIfAlternate tag type case foo: int case bar: TestStruct + if ['defined(TEST_IF_ALT_BAR)'] if ['defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)'] object q_obj_TestIfCmd-arg member foo: TestIfStruct optional=False
Signature · 1 line
2.20.0.rc1
Re: [Qemu-devel] [PATCH for-4.0 v7 19/27] qapi: add 'if' to alternate members
Quoted 9 lines · Marc-André Lureau
Add 'if' key to alternate members:
{ 'alternate': 'TestIfAlternate', 'data':
{ 'alt': { 'type': 'TestStruct', 'if': 'COND' } } }Generated code is not changed by this patch but with "qapi: add #if conditions to generated code".
Signed-off-by Marc-André Lureau <[email protected]>
PATCH v7 20/27 tests/qapi: add command with condition on alternate argument
In reply to Marc-André LureauFor completeness.
--- tests/qapi-schema/qapi-schema-test.json | 3 +++ tests/qapi-schema/qapi-schema-test.out | 6 ++++++ 2 files changed, 9 insertions(+)
tests/qapi-schema/qapi-schema-test.json +3 -0
@@ -223,6 +223,9 @@ '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': 'defined(TEST_IF_CMD_BAR)' } },
tests/qapi-schema/qapi-schema-test.out +6 -0
@@ -302,6 +302,12 @@ alternate TestIfAlternate case bar: TestStruct if ['defined(TEST_IF_ALT_BAR)'] if ['defined(TEST_IF_ALT) && defined(TEST_IF_STRUCT)'] +object q_obj_TestIfAlternateCmd-arg + member alt_cmd_arg: TestIfAlternate optional=False + if ['defined(TEST_IF_ALT)'] +command TestIfAlternateCmd q_obj_TestIfAlternateCmd-arg -> None + gen=True success_response=True boxed=False oob=False preconfig=False + if ['defined(TEST_IF_ALT)'] object q_obj_TestIfCmd-arg member foo: TestIfStruct optional=False member bar: TestIfEnum optional=False
Signature · 1 line
2.20.0.rc1
Re: [Qemu-devel] [PATCH for-4.0 v7 20/27] tests/qapi: add command with condition on alternate argument
Quoted 3 lines · Marc-André Lureau
For completeness.
Signed-off-by Marc-André Lureau <[email protected]>
Let's say "Forgotten in commit 967c885108f."
We could squash PATCH 18 and this one together. In case we don't:
PATCH v7 21/27 qapi: add #if conditions to generated code members
In reply to Marc-André LureauWrap generated enum/struct members and code with #if/#endif, using the .ifcond members added in the previous patches.
Some types generate both enum and struct members for example, so a step-by-step is unnecessarily complicated to deal with (it would easily generate invalid intermediary code).
--- scripts/qapi/common.py | 4 ++++ scripts/qapi/introspect.py | 13 +++++++++---- scripts/qapi/types.py | 4 ++++ scripts/qapi/visit.py | 6 ++++++ 4 files changed, 23 insertions(+), 4 deletions(-)
scripts/qapi/common.py +4 -0
@@ -2108,11 +2108,13 @@ const QEnumLookup %(c_name)s_lookup = { ''', c_name=c_name(name)) for m in members: + ret += gen_if(m.ifcond) index = c_enum_const(name, m.name, prefix) ret += mcgen(''' [%(index)s] = "%(name)s", ''', index=index, name=m.name) + ret += gen_endif(m.ifcond) ret += mcgen(''' },
@@ -2134,10 +2136,12 @@ typedef enum %(c_name)s { c_name=c_name(name)) for m in enum_members: + ret += gen_if(m.ifcond) ret += mcgen(''' %(c_enum)s, ''', c_enum=c_enum_const(name, m.name, prefix)) + ret += gen_endif(m.ifcond) ret += mcgen(''' } %(c_name)s;
scripts/qapi/introspect.py +9 -4
@@ -162,6 +162,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, {'if': member.ifcond}) return ret def _gen_variants(self, tag_name, variants):
@@ -169,14 +171,16 @@ 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)}, + {'if': variant.ifcond}) def visit_builtin_type(self, name, info, json_type): self._gen_qlit(name, 'builtin', {'json-type': json_type}, []) def visit_enum_type(self, name, info, ifcond, members, prefix): self._gen_qlit(name, 'enum', - {'values': [m.name for m in members]}, ifcond) + {'values': [(m.name, {'if': m.ifcond}) for m in members]}, + ifcond) def visit_array_type(self, name, info, ifcond, element_type): element = self._use_type(element_type)
@@ -192,8 +196,9 @@ const QLitObject %(c_name)s = %(c_string)s; def visit_alternate_type(self, name, info, ifcond, variants): self._gen_qlit(name, 'alternate', - {'members': [{'type': self._use_type(m.type)} - for m in variants.variants]}, ifcond) + {'members': [ + ({'type': self._use_type(m.type)}, {'if': m.ifcond}) + for m in variants.variants]}, ifcond) def visit_command(self, name, info, ifcond, arg_type, ret_type, gen, success_response, boxed, allow_oob, allow_preconfig):
scripts/qapi/types.py +4 -0
@@ -43,6 +43,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;
@@ -52,6 +53,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
@@ -131,11 +133,13 @@ def gen_variants(variants): for var in variants.variants: if var.type.name == 'q_empty': continue + 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 +6 -0
@@ -54,6 +54,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)) {
@@ -73,6 +74,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('''
@@ -84,6 +86,7 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) case_str = c_enum_const(variants.tag_member.type.name, var.name, variants.tag_member.type.prefix) + ret += gen_if(var.ifcond) if var.type.name == 'q_empty': # valid variant and nothing to do ret += mcgen('''
@@ -100,6 +103,7 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) case=case_str, c_type=var.type.c_name(), c_name=c_name(var.name)) + ret += gen_endif(var.ifcond) ret += mcgen(''' default: abort();
@@ -190,6 +194,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: ''',
@@ -217,6 +222,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:
Signature · 1 line
2.20.0.rc1
Re: [Qemu-devel] [PATCH for-4.0 v7 21/27] qapi: add #if conditions to generated code members
Quoted 8 lines · Marc-André Lureau
Wrap generated enum/struct members and code with #if/#endif, using the .ifcond members added in the previous patches.
Some types generate both enum and struct members for example, so a step-by-step is unnecessarily complicated to deal with (it would easily generate invalid intermediary code).
Signed-off-by Marc-André Lureau <[email protected]>
We've since agreed to tweak the commit message:
qapi: Add #if conditions to generated code members
Wrap generated enum and struct members and their supporting code with #if/#endif, using the .ifcond members added in the previous patches.
We do enum and struct in a single patch because union tag enum and the associated variants tie them together, and dealing with that to split the patch doesn't seem worthwhile.
Quoted 71 lines
--- scripts/qapi/common.py | 4 ++++ scripts/qapi/introspect.py | 13 +++++++++---- scripts/qapi/types.py | 4 ++++ scripts/qapi/visit.py | 6 ++++++ 4 files changed, 23 insertions(+), 4 deletions(-)scripts/qapi/common.py +4
-0@@ -2108,11 +2108,13 @@ const QEnumLookup %(c_name)s_lookup = { ''', c_name=c_name(name)) for m in members: + ret += gen_if(m.ifcond) index = c_enum_const(name, m.name, prefix) ret += mcgen(''' [%(index)s] = "%(name)s", ''', index=index, name=m.name) + ret += gen_endif(m.ifcond) ret += mcgen(''' },@@ -2134,10 +2136,12 @@ typedef enum %(c_name)s { c_name=c_name(name)) for m in enum_members: + ret += gen_if(m.ifcond) ret += mcgen(''' %(c_enum)s, ''', c_enum=c_enum_const(name, m.name, prefix)) + ret += gen_endif(m.ifcond) ret += mcgen(''' } %(c_name)s;scripts/qapi/introspect.py +6
-2@@ -162,6 +162,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, {'if': member.ifcond}) return ret def _gen_variants(self, tag_name, variants):@@ -169,14 +171,16 @@ 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)}, + {'if': variant.ifcond}) def visit_builtin_type(self, name, info, json_type): self._gen_qlit(name, 'builtin', {'json-type': json_type}, []) def visit_enum_type(self, name, info, ifcond, members, prefix): self._gen_qlit(name, 'enum', - {'values': [m.name for m in members]}, ifcond) + {'values': [(m.name, {'if': m.ifcond}) for m in members]}, + ifcond) def visit_array_type(self, name, info, ifcond, element_type): element = self._use_type(element_type)
scripts/qapi/introspect.py const QLitObject %(c_name)s = %(c_string)s; · line 200
Quoted 7 lines
scripts/qapi/introspect.py +2
-2@@ -192,8 +196,9 @@ const QLitObject %(c_name)s = %(c_string)s; def visit_alternate_type(self, name, info, ifcond, variants): self._gen_qlit(name, 'alternate', - {'members': [{'type': self._use_type(m.type)} - for m in variants.variants]}, ifcond) + {'members': [ + ({'type': self._use_type(m.type)}, {'if': m.ifcond})
Long line. I'll feed the series to pycodestyle and pylint.
Quoted 83 lines
scripts/qapi/introspect.py +1
-0+ for m in variants.variants]}, ifcond) def visit_command(self, name, info, ifcond, arg_type, ret_type, gen, success_response, boxed, allow_oob, allow_preconfig):scripts/qapi/types.py +4
-0@@ -43,6 +43,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;@@ -52,6 +53,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@@ -131,11 +133,13 @@ def gen_variants(variants): for var in variants.variants: if var.type.name == 'q_empty': continue + 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 +5
-0@@ -54,6 +54,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)) {@@ -73,6 +74,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('''@@ -84,6 +86,7 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) case_str = c_enum_const(variants.tag_member.type.name, var.name, variants.tag_member.type.prefix) + ret += gen_if(var.ifcond) if var.type.name == 'q_empty': # valid variant and nothing to do ret += mcgen('''@@ -100,6 +103,7 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) case=case_str, c_type=var.type.c_name(), c_name=c_name(var.name)) + ret += gen_endif(var.ifcond) ret += mcgen(''' default: abort();@@ -190,6 +194,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: ''',
scripts/qapi/visit.py void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error · line 228
Quoted 7 lines
scripts/qapi/visit.py +1
-0@@ -217,6 +222,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:
With the tweaked commit message:
PATCH v7 22/27 qapi: add 'If:' condition to enum values documentation
In reply to Marc-André LureauUse a common function to generate the "If:..." line.
While at it, get rid of the existing \n\n (no idea why it was there). Use a line-break in member description, this seems to look slightly better in the plaintext version.
--- scripts/qapi/doc.py | 24 +++++++++++++++--------- tests/qapi-schema/doc-good.json | 4 +++- tests/qapi-schema/doc-good.out | 1 + tests/qapi-schema/doc-good.texi | 2 +- 4 files changed, 20 insertions(+), 11 deletions(-)
scripts/qapi/doc.py +15 -9
@@ -126,19 +126,26 @@ def texi_body(doc): return texi_format(doc.body.text) -def texi_enum_value(value): +def texi_if(ifcond, prefix='\n', suffix='\n'): + """Format the #if condition""" + return '%s@b{If:} @code{%s}%s' % ( + prefix, ', '.join(ifcond), suffix) if ifcond else '' + + +def texi_enum_value(value, desc, suffix=''): """Format a table of members item for an enumeration value""" - return '@item @code{%s}\n' % value.name + return '@item @code{%s}\n%s%s' % ( + value.name, desc, texi_if(value.ifcond, prefix='@*')) -def texi_member(member, suffix=''): +def texi_member(member, desc='', suffix=''): """Format a table of members item for an object type member""" typ = member.type.doc_type() membertype = ': ' + typ if typ else '' - return '@item @code{%s%s}%s%s\n' % ( + return '@item @code{%s%s}%s%s\n%s' % ( member.name, membertype, ' (optional)' if member.optional else '', - suffix) + suffix, desc) def texi_members(doc, what, base, variants, member_func):
@@ -155,7 +162,7 @@ def texi_members(doc, what, base, variants, member_func): desc = 'One of ' + members_text + '\n' else: desc = 'Not documented\n' - items += member_func(section.member) + desc + items += member_func(section.member, desc) if base: items += '@item The members of @code{%s}\n' % base.doc_type() if variants:
@@ -165,7 +172,7 @@ def texi_members(doc, what, base, variants, member_func): if v.type.is_implicit(): assert not v.type.base and not v.type.variants for m in v.type.local_members: - items += member_func(m, when) + items += member_func(m, suffix=when) else: items += '@item The members of @code{%s}%s\n' % ( v.type.doc_type(), when)
@@ -185,8 +192,7 @@ def texi_sections(doc, ifcond): body += texi_example(section.text) else: body += texi_format(section.text) - if ifcond: - body += '\n\n@b{If:} @code{%s}' % ", ".join(ifcond) + body += texi_if(ifcond, suffix='') return body
tests/qapi-schema/doc-good.json +3 -1
@@ -55,7 +55,9 @@ # # @two is undocumented ## -{ 'enum': 'Enum', 'data': [ 'one', 'two' ], 'if': 'defined(IFCOND)' } +{ 'enum': 'Enum', 'data': + [ { 'name': 'one', 'if': 'defined(IFENUM)' }, 'two' ], + 'if': 'defined(IFCOND)' } ## # @Base:
tests/qapi-schema/doc-good.out +1 -0
@@ -11,6 +11,7 @@ enum QType module doc-good.json enum Enum member one + if ['defined(IFENUM)'] member two if ['defined(IFCOND)'] object Base
tests/qapi-schema/doc-good.texi +1 -1
@@ -84,12 +84,12 @@ Examples: @table @asis @item @code{one} The @emph{one} @{and only@} +@*@b{If:} @code{defined(IFENUM)} @item @code{two} Not documented @end table @code{two} is undocumented - @b{If:} @code{defined(IFCOND)} @end deftp
Signature · 1 line
2.20.0.rc1
Re: [Qemu-devel] [PATCH for-4.0 v7 22/27] qapi: add 'If:' condition to enum values documentation
Quoted 5 lines · Marc-André Lureau
Use a common function to generate the "If:..." line.
While at it, get rid of the existing \n\n (no idea why it was there). Use a line-break in member description, this seems to look slightly better in the plaintext version.
Where exactly in the patch is this done?
Quoted 13 lines
Signed-off-by Marc-André Lureau <[email protected]>--- scripts/qapi/doc.py | 24 +++++++++++++++--------- tests/qapi-schema/doc-good.json | 4 +++- tests/qapi-schema/doc-good.out | 1 + tests/qapi-schema/doc-good.texi | 2 +- 4 files changed, 20 insertions(+), 11 deletions(-)scripts/qapi/doc.py +0
-0
scripts/qapi/doc.py def texi_body(doc): · line 132
Quoted 8 lines
scripts/qapi/doc.py +4
-1@@ -126,19 +126,26 @@ def texi_body(doc): return texi_format(doc.body.text) -def texi_enum_value(value): +def texi_if(ifcond, prefix='\n', suffix='\n'): + """Format the #if condition""" + return '%s@b{If:} @code{%s}%s' % ( + prefix, ', '.join(ifcond), suffix) if ifcond else ''
I like the ternary operator, but I nevertheless think this function is easier to read as
if not ifcond:
return ''
return '%s@b{If:} @code{%s}%s' % (prefix, ', '.join(ifcond), suffix)
scripts/qapi/doc.py def texi_body(doc): · line 138
Quoted 7 lines
scripts/qapi/doc.py +5
-1+ + +def texi_enum_value(value, desc, suffix=''): """Format a table of members item for an enumeration value""" - return '@item @code{%s}\n' % value.name + return '@item @code{%s}\n%s%s' % ( + value.name, desc, texi_if(value.ifcond, prefix='@*'))
Do you ignore suffix intentionally?
Quoted 17 lines
scripts/qapi/doc.py +3
-3-def texi_member(member, suffix=''): +def texi_member(member, desc='', suffix=''): """Format a table of members item for an object type member""" typ = member.type.doc_type() membertype = ': ' + typ if typ else '' - return '@item @code{%s%s}%s%s\n' % ( + return '@item @code{%s%s}%s%s\n%s' % ( member.name, membertype, ' (optional)' if member.optional else '', - suffix) + suffix, desc) def texi_members(doc, what, base, variants, member_func):
scripts/qapi/doc.py def texi_members(doc, what, base, variants, member_func): · line 165
Quoted 5 lines
scripts/qapi/doc.py +1
-1@@ -155,7 +162,7 @@ def texi_members(doc, what, base, variants, member_func): desc = 'One of ' + members_text + '\n' else: desc = 'Not documented\n' - items += member_func(section.member) + desc + items += member_func(section.member, desc)
Here, you pass a @desc argument, but no @suffix.
@member_func is either texi_enum_value() or texi_member(). Works.
scripts/qapi/doc.py def texi_members(doc, what, base, variants, member_func): · line 175
Quoted 9 lines
scripts/qapi/doc.py +1
-1if base: items += '@item The members of @code{%s}\n' % base.doc_type() if variants:@@ -165,7 +172,7 @@ def texi_members(doc, what, base, variants, member_func): if v.type.is_implicit(): assert not v.type.base and not v.type.variants for m in v.type.local_members: - items += member_func(m, when) + items += member_func(m, suffix=when)
Here, you pass a @suffix argument, but no @desc.
texi_enum_value() would choke on that, but it can't occur here, because enums have no variants.
Still, I'd prefer texi_enum_value() and texi_member() to have the exact same signature.
Quoted 18 lines
scripts/qapi/doc.py +1
-2else: items += '@item The members of @code{%s}%s\n' % ( v.type.doc_type(), when)@@ -185,8 +192,7 @@ def texi_sections(doc, ifcond): body += texi_example(section.text) else: body += texi_format(section.text) - if ifcond: - body += '\n\n@b{If:} @code{%s}' % ", ".join(ifcond) + body += texi_if(ifcond, suffix='') return bodytests/qapi-schema/doc-good.json +0
-0
tests/qapi-schema/doc-good.json line 60
Quoted 7 lines
tests/qapi-schema/doc-good.json +3
-1@@ -55,7 +55,9 @@ # # @two is undocumented ## -{ 'enum': 'Enum', 'data': [ 'one', 'two' ], 'if': 'defined(IFCOND)' } +{ 'enum': 'Enum', 'data': + [ { 'name': 'one', 'if': 'defined(IFENUM)' }, 'two' ], + 'if': 'defined(IFCOND)' }
IFCOND applies to the enum, and IFENUM applies to one of its values. Awkward. Let's rename IFENUM to IFONE.
Quoted 20 lines
tests/qapi-schema/doc-good.json +0
-0## # @Base:tests/qapi-schema/doc-good.out +1
-0@@ -11,6 +11,7 @@ enum QType module doc-good.json enum Enum member one + if ['defined(IFENUM)'] member two if ['defined(IFCOND)'] object Basetests/qapi-schema/doc-good.texi +0
-0
tests/qapi-schema/doc-good.texi Examples: · line 87
Quoted 4 lines
tests/qapi-schema/doc-good.texi +1
-0@@ -84,12 +84,12 @@ Examples: @table @asis @item @code{one} The @emph{one} @{and only@} +@*@b{If:} @code{defined(IFENUM)}
@* forces a line break. Putting it at the end of the previous line would be tidier, but let's ignore that for now.
Quoted 8 lines
tests/qapi-schema/doc-good.texi +0
-1@item @code{two} Not documented @end table @code{two} is undocumented - @b{If:} @code{defined(IFCOND)} @end deftp
Re: [Qemu-devel] [PATCH for-4.0 v7 22/27] qapi: add 'If:' condition to enum values documentation
Hi
Quoted 10 lines · Markus Armbruster
Quoted 5 lines · Marc-André Lureau
Use a common function to generate the "If:..." line.
While at it, get rid of the existing \n\n (no idea why it was there). Use a line-break in member description, this seems to look slightly better in the plaintext version.
Where exactly in the patch is this done?
- if ifcond:
- body += '\n\n@b{If:} @code{%s}' % ", ".join(ifcond)
+ body += texi_if(ifcond, suffix='')
See also doc-good.texi output
Quoted 26 lines
Quoted 14 lines
Signed-off-by Marc-André Lureau <[email protected]>--- scripts/qapi/doc.py | 24 +++++++++++++++--------- tests/qapi-schema/doc-good.json | 4 +++- tests/qapi-schema/doc-good.out | 1 + tests/qapi-schema/doc-good.texi | 2 +- 4 files changed, 20 insertions(+), 11 deletions(-)scripts/qapi/doc.py +0
-0scripts/qapi/doc.py def texi_body(doc): · line 132
Quoted 8 lines
scripts/qapi/doc.py +4
-1@@ -126,19 +126,26 @@ def texi_body(doc): return texi_format(doc.body.text) -def texi_enum_value(value): +def texi_if(ifcond, prefix='\n', suffix='\n'): + """Format the #if condition""" + return '%s@b{If:} @code{%s}%s' % ( + prefix, ', '.join(ifcond), suffix) if ifcond else ''I like the ternary operator, but I nevertheless think this function is easier to read as
Quoted 4 lines
if not ifcond:
return ''
return '%s@b{If:} @code{%s}%s' % (prefix, ', '.join(ifcond), suffix)
No difference to me, sure
Quoted 9 lines
scripts/qapi/doc.py def texi_body(doc): · line 138
Quoted 7 lines
scripts/qapi/doc.py +5
-1+ + +def texi_enum_value(value, desc, suffix=''): """Format a table of members item for an enumeration value""" - return '@item @code{%s}\n' % value.name + return '@item @code{%s}\n%s%s' % ( + value.name, desc, texi_if(value.ifcond, prefix='@*'))Do you ignore suffix intentionally?
yes
Quoted 40 lines
Quoted 18 lines
scripts/qapi/doc.py +3
-3-def texi_member(member, suffix=''): +def texi_member(member, desc='', suffix=''): """Format a table of members item for an object type member""" typ = member.type.doc_type() membertype = ': ' + typ if typ else '' - return '@item @code{%s%s}%s%s\n' % ( + return '@item @code{%s%s}%s%s\n%s' % ( member.name, membertype, ' (optional)' if member.optional else '', - suffix) + suffix, desc) def texi_members(doc, what, base, variants, member_func):scripts/qapi/doc.py def texi_members(doc, what, base, variants, member_func): · line 165
Quoted 5 lines
scripts/qapi/doc.py +1
-1@@ -155,7 +162,7 @@ def texi_members(doc, what, base, variants, member_func): desc = 'One of ' + members_text + '\n' else: desc = 'Not documented\n' - items += member_func(section.member) + desc + items += member_func(section.member, desc)Here, you pass a @desc argument, but no @suffix.
@member_func is either
texi_enum_value()ortexi_member(). Works.scripts/qapi/doc.py def texi_members(doc, what, base, variants, member_func): · line 175
Quoted 9 lines
scripts/qapi/doc.py +1
-1if base: items += '@item The members of @code{%s}\n' % base.doc_type() if variants:@@ -165,7 +172,7 @@ def texi_members(doc, what, base, variants, member_func): if v.type.is_implicit(): assert not v.type.base and not v.type.variants for m in v.type.local_members: - items += member_func(m, when) + items += member_func(m, suffix=when)Here, you pass a @suffix argument, but no @desc.
Quoted 5 lines
texi_enum_value()would choke on that, but it can't occur here, because enums have no variants.Still, I'd prefer
texi_enum_value()andtexi_member()to have the exact same signature.
OK (not sure what is the more pythonic way)
Quoted 14 lines
Quoted 14 lines
scripts/qapi/doc.py +1
-2else: items += '@item The members of @code{%s}%s\n' % ( v.type.doc_type(), when)@@ -185,8 +192,7 @@ def texi_sections(doc, ifcond): body += texi_example(section.text) else: body += texi_format(section.text) - if ifcond: - body += '\n\n@b{If:} @code{%s}' % ", ".join(ifcond) + body += texi_if(ifcond, suffix='') return body
Quoted 15 lines
tests/qapi-schema/doc-good.json line 60
Quoted 12 lines
tests/qapi-schema/doc-good.json +3
-1@@ -55,7 +55,9 @@ # # @two is undocumented ## -{ 'enum': 'Enum', 'data': [ 'one', 'two' ], 'if': 'defined(IFCOND)' } +{ 'enum': 'Enum', 'data': + [ { 'name': 'one', 'if': 'defined(IFENUM)' }, 'two' ], + 'if': 'defined(IFCOND)' }IFCOND applies to the enum, and IFENUM applies to one of its values. Awkward. Let's rename IFENUM to IFONE.
ok
Quoted 20 lines
Quoted 20 lines
tests/qapi-schema/doc-good.json +0
-0## # @Base:tests/qapi-schema/doc-good.out +1
-0@@ -11,6 +11,7 @@ enum QType module doc-good.json enum Enum member one + if ['defined(IFENUM)'] member two if ['defined(IFCOND)'] object Basetests/qapi-schema/doc-good.texi +0
-0
Quoted 8 lines
tests/qapi-schema/doc-good.texi Examples: · line 87
Quoted 5 lines
tests/qapi-schema/doc-good.texi +1
-0@@ -84,12 +84,12 @@ Examples: @table @asis @item @code{one} The @emph{one} @{and only@} +@*@b{If:} @code{defined(IFENUM)}@* forces a line break. Putting it at the end of the previous line would be tidier, but let's ignore that for now.
For consistency and easier reading, I think it's better to keep it as a seperate line in the end for the corresponding section
Quoted 9 lines
tests/qapi-schema/doc-good.texi Examples: · line 94
Quoted 9 lines
tests/qapi-schema/doc-good.texi +0
-1@item @code{two} Not documented @end table @code{two} is undocumented - @b{If:} @code{defined(IFCOND)} @end deftp
thanks
Re: [Qemu-devel] [PATCH for-4.0 v7 22/27] qapi: add 'If:' condition to enum values documentation
Quoted 14 lines · Marc-André Lureau
Hi
Quoted 10 lines · Markus Armbruster
Quoted 5 lines · Marc-André Lureau
Use a common function to generate the "If:..." line.
While at it, get rid of the existing \n\n (no idea why it was there). Use a line-break in member description, this seems to look slightly better in the plaintext version.
Where exactly in the patch is this done?
scripts/qapi/doc.py def texi_sections(doc, ifcond): · line 195
Quoted 3 lines
scripts/qapi/doc.py +1
-2- if ifcond: - body += '\n\n@b{If:} @code{%s}' % ", ".join(ifcond) + body += texi_if(ifcond, suffix='')
Thanks. I suspected that much, but wasn't sure.
Quoted 45 lines
See also doc-good.texi output
Quoted 26 lines
Quoted 14 lines
Signed-off-by Marc-André Lureau <[email protected]>--- scripts/qapi/doc.py | 24 +++++++++++++++--------- tests/qapi-schema/doc-good.json | 4 +++- tests/qapi-schema/doc-good.out | 1 + tests/qapi-schema/doc-good.texi | 2 +- 4 files changed, 20 insertions(+), 11 deletions(-)scripts/qapi/doc.py +0
-0scripts/qapi/doc.py def texi_body(doc): · line 132
Quoted 8 lines
scripts/qapi/doc.py +4
-1@@ -126,19 +126,26 @@ def texi_body(doc): return texi_format(doc.body.text) -def texi_enum_value(value): +def texi_if(ifcond, prefix='\n', suffix='\n'): + """Format the #if condition""" + return '%s@b{If:} @code{%s}%s' % ( + prefix, ', '.join(ifcond), suffix) if ifcond else ''I like the ternary operator, but I nevertheless think this function is easier to read as
Quoted 4 lines
if not ifcond:
return ''
return '%s@b{If:} @code{%s}%s' % (prefix, ', '.join(ifcond), suffix)No difference to me, sure
Quoted 9 lines
scripts/qapi/doc.py def texi_body(doc): · line 138
Quoted 7 lines
scripts/qapi/doc.py +5
-1+ + +def texi_enum_value(value, desc, suffix=''): """Format a table of members item for an enumeration value""" - return '@item @code{%s}\n' % value.name + return '@item @code{%s}\n%s%s' % ( + value.name, desc, texi_if(value.ifcond, prefix='@*'))Do you ignore suffix intentionally?
Quoted 1 line
yes
That's kind of surprising, until you pry open the abstraction and realize that we reach this one only from the first call of member_func() below, which ...
Quoted 26 lines
Quoted 26 lines
Quoted 18 lines
scripts/qapi/doc.py +3
-3-def texi_member(member, suffix=''): +def texi_member(member, desc='', suffix=''): """Format a table of members item for an object type member""" typ = member.type.doc_type() membertype = ': ' + typ if typ else '' - return '@item @code{%s%s}%s%s\n' % ( + return '@item @code{%s%s}%s%s\n%s' % ( member.name, membertype, ' (optional)' if member.optional else '', - suffix) + suffix, desc) def texi_members(doc, what, base, variants, member_func):scripts/qapi/doc.py def texi_members(doc, what, base, variants, member_func): · line 165
Quoted 5 lines
scripts/qapi/doc.py +1
-1@@ -155,7 +162,7 @@ def texi_members(doc, what, base, variants, member_func): desc = 'One of ' + members_text + '\n' else: desc = 'Not documented\n' - items += member_func(section.member) + desc + items += member_func(section.member, desc)Here, you pass a @desc argument, but no @suffix.
Quoted 1 line
Quoted 1 line
@member_func is either
texi_enum_value()ortexi_member(). Works.
... doesn't pass @suffix. So @suffix is empty when we ignore it.
Quoted 12 lines
Quoted 12 lines
scripts/qapi/doc.py def texi_members(doc, what, base, variants, member_func): · line 175
Quoted 9 lines
scripts/qapi/doc.py +1
-1if base: items += '@item The members of @code{%s}\n' % base.doc_type() if variants:@@ -165,7 +172,7 @@ def texi_members(doc, what, base, variants, member_func): if v.type.is_implicit(): assert not v.type.base and not v.type.variants for m in v.type.local_members: - items += member_func(m, when) + items += member_func(m, suffix=when)Here, you pass a @suffix argument, but no @desc.
Quoted 7 lines
Quoted 5 lines
texi_enum_value()would choke on that, but it can't occur here, because enums have no variants.Still, I'd prefer
texi_enum_value()andtexi_member()to have the exact same signature.OK (not sure what is the more pythonic way)
I suspect there's a clean solution struggling to get out. But I can't grasp it yet.
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
scripts/qapi/doc.py +1
-2else: items += '@item The members of @code{%s}%s\n' % ( v.type.doc_type(), when)@@ -185,8 +192,7 @@ def texi_sections(doc, ifcond): body += texi_example(section.text) else: body += texi_format(section.text) - if ifcond: - body += '\n\n@b{If:} @code{%s}' % ", ".join(ifcond) + body += texi_if(ifcond, suffix='') return body
[...]
PATCH v7 23/27 qapi: add 'If:' condition to struct members documentation
In reply to Marc-André Lureau--- scripts/qapi/doc.py | 4 ++-- tests/qapi-schema/doc-good.json | 3 ++- tests/qapi-schema/doc-good.out | 1 + tests/qapi-schema/doc-good.texi | 1 + 4 files changed, 6 insertions(+), 3 deletions(-)
scripts/qapi/doc.py +2 -2
@@ -142,10 +142,10 @@ def texi_member(member, desc='', suffix=''): """Format a table of members item for an object type member""" typ = member.type.doc_type() membertype = ': ' + typ if typ else '' - return '@item @code{%s%s}%s%s\n%s' % ( + return '@item @code{%s%s}%s%s\n%s%s' % ( member.name, membertype, ' (optional)' if member.optional else '', - suffix, desc) + suffix, desc, texi_if(member.ifcond, prefix='@*')) def texi_members(doc, what, base, variants, member_func):
tests/qapi-schema/doc-good.json +2 -1
@@ -72,7 +72,8 @@ # # Another paragraph (but no @var: line) ## -{ 'struct': 'Variant1', 'data': { 'var1': 'str' } } +{ 'struct': 'Variant1', + 'data': { 'var1': { 'type': 'str', 'if': 'defined(IFSTR)' } } } ## # @Variant2:
tests/qapi-schema/doc-good.out +1 -0
@@ -18,6 +18,7 @@ object Base member base1: Enum optional=False object Variant1 member var1: str optional=False + if ['defined(IFSTR)'] object Variant2 object Object base Base
tests/qapi-schema/doc-good.texi +1 -0
@@ -119,6 +119,7 @@ Another paragraph (but no @code{var}: line) @table @asis @item @code{var1: string} Not documented +@*@b{If:} @code{defined(IFSTR)} @end table @end deftp
Signature · 1 line
2.20.0.rc1
PATCH v7 24/27 qapi: add condition to variants documentation
In reply to Marc-André Lureau--- scripts/qapi/doc.py | 4 ++-- tests/qapi-schema/doc-good.json | 4 ++-- tests/qapi-schema/doc-good.out | 3 +++ tests/qapi-schema/doc-good.texi | 4 ++-- 4 files changed, 9 insertions(+), 6 deletions(-)
scripts/qapi/doc.py +2 -2
@@ -167,8 +167,8 @@ 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, texi_if(v.ifcond, " (", ")")) if v.type.is_implicit(): assert not v.type.base and not v.type.variants for m in v.type.local_members:
tests/qapi-schema/doc-good.json +2 -2
@@ -86,13 +86,13 @@ { 'union': 'Object', 'base': 'Base', 'discriminator': 'base1', - 'data': { 'one': 'Variant1', 'two': 'Variant2' } } + 'data': { 'one': 'Variant1', 'two': { 'type': 'Variant2', 'if': 'IFTWO' } } } ## # @SugaredUnion: ## { 'union': 'SugaredUnion', - 'data': { 'one': 'Variant1', 'two': 'Variant2' } } + 'data': { 'one': 'Variant1', 'two': { 'type': 'Variant2', 'if': 'IFTWO' } } } ## # == Another subsection
tests/qapi-schema/doc-good.out +3 -0
@@ -25,6 +25,7 @@ object Object tag base1 case one: Variant1 case two: Variant2 + if ['IFTWO'] object q_obj_Variant1-wrapper member data: Variant1 optional=False object q_obj_Variant2-wrapper
@@ -32,11 +33,13 @@ object q_obj_Variant2-wrapper enum SugaredUnionKind member one member two + if ['IFTWO'] object SugaredUnion member type: SugaredUnionKind optional=False tag type case one: q_obj_Variant1-wrapper case two: q_obj_Variant2-wrapper + if ['IFTWO'] object q_obj_cmd-arg member arg1: int optional=False member arg2: str optional=True
tests/qapi-schema/doc-good.texi +2 -2
@@ -142,7 +142,7 @@ Not documented @table @asis @item The members of @code{Base} @item The members of @code{Variant1} when @code{base1} is @t{"one"} -@item The members of @code{Variant2} when @code{base1} is @t{"two"} +@item The members of @code{Variant2} when @code{base1} is @t{"two"} (@b{If:} @code{IFTWO}) @end table @end deftp
@@ -158,7 +158,7 @@ Not documented @item @code{type} One of @t{"one"}, @t{"two"} @item @code{data: Variant1} when @code{type} is @t{"one"} -@item @code{data: Variant2} when @code{type} is @t{"two"} +@item @code{data: Variant2} when @code{type} is @t{"two"} (@b{If:} @code{IFTWO}) @end table @end deftp
Signature · 1 line
2.20.0.rc1
PATCH v7 25/27 qapi: break long lines at 'data' member
In reply to Marc-André LureauLet's break the line before 'data'. While at it, improve a bit indentation/spacing. (I removed some alignment which are not helping much readability and become quickly inconsistent)
--- qapi/block-core.json | 13 ++-- qapi/char.json | 138 +++++++++++++++++++++++++------------------ qapi/migration.json | 3 +- qapi/misc.json | 7 ++- qapi/net.json | 3 +- qapi/tpm.json | 5 +- qapi/ui.json | 3 +- 7 files changed, 101 insertions(+), 71 deletions(-)
qapi/block-core.json +8 -5
@@ -1143,8 +1143,10 @@ # This command is now obsolete and will always return an error since 2.10 # ## -{ 'command': 'block_passwd', 'data': {'*device': 'str', - '*node-name': 'str', 'password': 'str'} } +{ 'command': 'block_passwd', + 'data': { '*device': 'str', + '*node-name': 'str', + 'password': 'str' } } ## # @block_resize:
@@ -1171,9 +1173,10 @@ # <- { "return": {} } # ## -{ 'command': 'block_resize', 'data': { '*device': 'str', - '*node-name': 'str', - 'size': 'int' }} +{ 'command': 'block_resize', + 'data': { '*device': 'str', + '*node-name': 'str', + 'size': 'int' } } ## # @NewImageMode:
qapi/char.json +79 -59
257 lines not on this page · raw message
qapi/migration.json +2 -1
@@ -1356,7 +1356,8 @@ # # Since: 3.0 ## -{ 'command': 'migrate-recover', 'data': { 'uri': 'str' }, +{ 'command': 'migrate-recover', + 'data': { 'uri': 'str' }, 'allow-oob': true } ##
qapi/misc.json +5 -2
@@ -2381,7 +2381,9 @@ # <- { "return": { "fdset-id": 1, "fd": 3 } } # ## -{ 'command': 'add-fd', 'data': {'*fdset-id': 'int', '*opaque': 'str'}, +{ 'command': 'add-fd', + 'data': { '*fdset-id': 'int', + '*opaque': 'str' }, 'returns': 'AddfdInfo' } ##
@@ -2653,7 +2655,8 @@ # } # ## -{'command': 'query-command-line-options', 'data': { '*option': 'str' }, +{'command': 'query-command-line-options', + 'data': { '*option': 'str' }, 'returns': ['CommandLineOptionInfo'], 'allow-preconfig': true }
qapi/net.json +2 -1
@@ -657,7 +657,8 @@ # } # ## -{ 'command': 'query-rx-filter', 'data': { '*name': 'str' }, +{ 'command': 'query-rx-filter', + 'data': { '*name': 'str' }, 'returns': ['RxFilterInfo'] } ##
qapi/tpm.json +3 -2
@@ -76,8 +76,9 @@ # # Since: 1.5 ## -{ 'struct': 'TPMPassthroughOptions', 'data': { '*path' : 'str', - '*cancel-path' : 'str'} } +{ 'struct': 'TPMPassthroughOptions', + 'data': { '*path': 'str', + '*cancel-path': 'str' } } ## # @TPMEmulatorOptions:
qapi/ui.json +2 -1
@@ -598,7 +598,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)' } ##
Signature · 1 line
2.20.0.rc1
Re: [Qemu-devel] [PATCH for-4.0 v7 25/27] qapi: break long lines at 'data' member
Quoted 6 lines · Marc-André Lureau
Let's break the line before 'data'. While at it, improve a bit indentation/spacing. (I removed some alignment which are not helping much readability and become quickly inconsistent)
Suggested-by Markus Armbruster <[email protected]>Signed-off-by Marc-André Lureau <[email protected]>
I can cherry-pick this one into my next pull request to reduce the likelihood of conflicts.
Re: [Qemu-devel] [PATCH for-4.0 v7 25/27] qapi: break long lines at 'data' member
Hi
Quoted 7 lines · Markus Armbruster
Quoted 4 lines · Marc-André Lureau
Let's break the line before 'data'. While at it, improve a bit indentation/spacing. (I removed some alignment which are not helping much readability and become quickly inconsistent)
Quoted 7 lines
Quoted 2 lines
Suggested-by Markus Armbruster <[email protected]>Signed-off-by Marc-André Lureau <[email protected]>Reviewed-by Markus Armbruster <[email protected]>I can cherry-pick this one into my next pull request to reduce the likelihood of conflicts.
Ideally, you take as much patches as you can in your -next tree, so I don't have to carry them and resend them all the time. Up to you, you are the maintainer.
PATCH v7 26/27 qapi: add more conditions to SPICE
In reply to Marc-André LureauNow that member can be made conditional, let's make SPICE chardev conditional:
* spiceport, spicevmc
Before and after the patch for !CONFIG_SPICE, the error is the same ('spiceport' is not a valid char driver name).
--- qapi/char.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
qapi/char.json +8 -8
@@ -332,8 +332,8 @@ ## { 'struct': 'ChardevSpiceChannel', 'data': { 'type': 'str' }, - 'base': 'ChardevCommon' } -# TODO: 'if': 'defined(CONFIG_SPICE)' + 'base': 'ChardevCommon', + 'if': 'defined(CONFIG_SPICE)' } ## # @ChardevSpicePort:
@@ -346,8 +346,8 @@ ## { 'struct': 'ChardevSpicePort', 'data': { 'fqdn': 'str' }, - 'base': 'ChardevCommon' } -# TODO: 'if': 'defined(CONFIG_SPICE)' + 'base': 'ChardevCommon', + 'if': 'defined(CONFIG_SPICE)' } ## # @ChardevVC:
@@ -404,10 +404,10 @@ 'testdev': 'ChardevCommon', 'stdio': 'ChardevStdio', 'console': 'ChardevCommon', - 'spicevmc': 'ChardevSpiceChannel', -# TODO: { 'type': 'ChardevSpiceChannel', 'if': 'defined(CONFIG_SPICE)' }, - 'spiceport': 'ChardevSpicePort', -# TODO: { 'type': 'ChardevSpicePort', 'if': 'defined(CONFIG_SPICE)' }, + 'spicevmc': { 'type': 'ChardevSpiceChannel', + 'if': 'defined(CONFIG_SPICE)' }, + 'spiceport': { 'type': 'ChardevSpicePort', + 'if': 'defined(CONFIG_SPICE)' }, 'vc': 'ChardevVC', 'ringbuf': 'ChardevRingbuf', # next one is just for compatibility
Signature · 1 line
2.20.0.rc1
Re: [Qemu-devel] [PATCH for-4.0 v7 26/27] qapi: add more conditions to SPICE
Quoted 44 lines · Marc-André Lureau
Now that member can be made conditional, let's make SPICE chardev conditional:
* spiceport, spicevmc
Before and after the patch for !CONFIG_SPICE, the error is the same ('spiceport' is not a valid char driver name).
Signed-off-by Marc-André Lureau <[email protected]>--- qapi/char.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)qapi/char.json +4
-5@@ -332,8 +332,8 @@ ## { 'struct': 'ChardevSpiceChannel', 'data': { 'type': 'str' }, - 'base': 'ChardevCommon' } -# TODO: 'if': 'defined(CONFIG_SPICE)' + 'base': 'ChardevCommon', + 'if': 'defined(CONFIG_SPICE)' } ## # @ChardevSpicePort:@@ -346,8 +346,8 @@ ## { 'struct': 'ChardevSpicePort', 'data': { 'fqdn': 'str' }, - 'base': 'ChardevCommon' } -# TODO: 'if': 'defined(CONFIG_SPICE)' + 'base': 'ChardevCommon', + 'if': 'defined(CONFIG_SPICE)' } ## # @ChardevVC:@@ -404,10 +404,10 @@ 'testdev': 'ChardevCommon', 'stdio': 'ChardevStdio', 'console': 'ChardevCommon', - 'spicevmc': 'ChardevSpiceChannel',
qapi/char.json line 413
Quoted 10 lines
qapi/char.json +4
-3-# TODO: { 'type': 'ChardevSpiceChannel', 'if': 'defined(CONFIG_SPICE)' }, - 'spiceport': 'ChardevSpicePort', -# TODO: { 'type': 'ChardevSpicePort', 'if': 'defined(CONFIG_SPICE)' }, + 'spicevmc': { 'type': 'ChardevSpiceChannel', + 'if': 'defined(CONFIG_SPICE)' }, + 'spiceport': { 'type': 'ChardevSpicePort', + 'if': 'defined(CONFIG_SPICE)' }, 'vc': 'ChardevVC', 'ringbuf': 'ChardevRingbuf', # next one is just for compatibility
Reviewing this patch's effect on generated files led me to a few things. None of them need to be addressed to get this series accepted. I actually recommend not to address them in this series, because that could lead to further delays.
There's an opportunity for minor output beautification: back-to-back #endif / #if like
#endif /* defined(CONFIG_SPICE) */
#if defined(CONFIG_SPICE)in generated C could be omitted.
The generated documentation shows conditionals as
If: 'defined(CONFIG_SPICE)'
We should at least explain this notation in the introduction. Other notation, too. Predates this series.
Should we *strip* documentation for disabled stuff instead?
Complication: disabled everywhere, or just for a target? We build the documentation only once, and that's a feature. We can therefore only strip documentation for stuff that's disabled target-independently. That's a bit more involved.
Hi,
The cover letter title should be "qapi: add #if pre-processor conditions to generated code (part 2)"
Quoted 193 lines · Marc-André Lureau
This is the second part of the "add #if pre-processor conditions to generated code" series, adding schema member conditions (roughly 16-38/49).
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)' } ] }v7:
- replacedosdep.hinclude by a #warning check
- renamed event_names/event_enum_members, enum_name/event_enum_ename
- move normalization after checking
- simplifymake_enum_members()
- some tests changes
- rebase/conflicts tweaks
- squash documentation changes in related patches
- tweak generated documentation code & output
- modify commit comments, add rb tagsv6:
- subset of v5 series: add schema member conditions
- split "qapi: factor outcheck_known_keys()", error report improvements
- add a patch to "qapi: includeosdep.hin type headers" to avoid potential build configuration change issues
- rebased, on top of Markus's qapi-next branch (first 4 patches, included for patchew testing)Marc-André Lureau (27):
qapi: make sureosdep.his included in type headers
qapi: do not define enumeration value explicitly
qapi: rename QAPISchemaEnumType.values to .members
qapi: change enum visitor and gen_enum* to take QAPISchemaMember
tests: print enum type members more like object type members
qapi: factor out checking for keys
qapi: improve reporting of unknown or missing keys
qapi: add a dictionary form with 'name' key for enum members
qapi: add 'if' to enum members
qapi-events: add 'if' condition to implicit event enum
qapi: pass long form enum tomake_enum_members
qapi: renameallow_dicttoallow_implicit
qapi: add a dictionary form for TYPE
qapi: add 'if' to implicit struct members
qapi: add an error in case a discriminator is conditional
qapi: add 'if' to union members
qapi: simplifymake_enum_members()
tests/qapi: add command with condition on union argument
qapi: add 'if' to alternate members
tests/qapi: add command with condition on alternate argument
qapi: add #if conditions to generated code members
qapi: add 'If:' condition to enum values documentation
qapi: add 'If:' condition to struct members documentation
qapi: add condition to variants documentation
qapi: break long lines at 'data' member
qapi: add more conditions to SPICE
qapi: add conditions to REPLICATION type/commands on the schemaqapi/block-core.json | 26 +- qapi/char.json | 150 ++++++----- qapi/migration.json | 15 +- qapi/misc.json | 7 +- qapi/net.json | 3 +- qapi/tpm.json | 5 +- qapi/ui.json | 3 +- scripts/qapi/common.py | 253 ++++++++++++------ scripts/qapi/doc.py | 30 ++- scripts/qapi/events.py | 13 +- scripts/qapi/introspect.py | 16 +- scripts/qapi/types.py | 13 +- scripts/qapi/visit.py | 8 +- migration/colo.c | 16 +- monitor.c | 5 - docs/devel/qapi-code-gen.txt | 19 ++ tests/Makefile.include | 11 +- tests/qapi-schema/alternate-base.err | 1 + tests/qapi-schema/alternate-invalid-dict.err | 1 + ...ember.exit => alternate-invalid-dict.exit} | 0 tests/qapi-schema/alternate-invalid-dict.json | 4 + ...-member.out => alternate-invalid-dict.out} | 0 tests/qapi-schema/comments.out | 14 +- tests/qapi-schema/doc-bad-section.out | 13 +- tests/qapi-schema/doc-good.json | 11 +- tests/qapi-schema/doc-good.out | 22 +- tests/qapi-schema/doc-good.texi | 7 +- tests/qapi-schema/double-type.err | 1 + tests/qapi-schema/empty.out | 9 +- tests/qapi-schema/enum-bad-member.err | 1 + tests/qapi-schema/enum-bad-member.exit | 1 + tests/qapi-schema/enum-bad-member.json | 2 + tests/qapi-schema/enum-bad-member.out | 0 .../qapi-schema/enum-dict-member-unknown.err | 2 + .../qapi-schema/enum-dict-member-unknown.exit | 1 + .../qapi-schema/enum-dict-member-unknown.json | 2 + .../qapi-schema/enum-dict-member-unknown.out | 0 tests/qapi-schema/enum-dict-member.err | 1 - tests/qapi-schema/enum-dict-member.json | 2 - 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/event-case.out | 9 +- .../qapi-schema/event-member-invalid-dict.err | 1 + .../event-member-invalid-dict.exit | 1 + .../event-member-invalid-dict.json | 2 + .../qapi-schema/event-member-invalid-dict.out | 0 tests/qapi-schema/event-nest-struct.json | 2 +- .../flat-union-inline-invalid-dict.err | 1 + .../flat-union-inline-invalid-dict.exit | 1 + .../flat-union-inline-invalid-dict.json | 11 + .../flat-union-inline-invalid-dict.out | 0 tests/qapi-schema/flat-union-inline.json | 2 +- .../flat-union-invalid-if-discriminator.err | 1 + .../flat-union-invalid-if-discriminator.exit | 1 + .../flat-union-invalid-if-discriminator.json | 17 ++ .../flat-union-invalid-if-discriminator.out | 0 tests/qapi-schema/ident-with-escape.out | 9 +- tests/qapi-schema/include-relpath.out | 14 +- tests/qapi-schema/include-repetition.out | 14 +- tests/qapi-schema/include-simple.out | 14 +- tests/qapi-schema/indented-expr.out | 9 +- .../nested-struct-data-invalid-dict.err | 1 + .../nested-struct-data-invalid-dict.exit | 1 + .../nested-struct-data-invalid-dict.json | 3 + .../nested-struct-data-invalid-dict.out | 0 tests/qapi-schema/nested-struct-data.json | 2 +- tests/qapi-schema/qapi-schema-test.json | 39 ++- tests/qapi-schema/qapi-schema-test.out | 74 ++++- .../struct-member-invalid-dict.err | 1 + .../struct-member-invalid-dict.exit | 1 + .../struct-member-invalid-dict.json | 3 + .../struct-member-invalid-dict.out | 0 tests/qapi-schema/test-qapi.py | 9 +- .../qapi-schema/union-branch-invalid-dict.err | 1 + .../union-branch-invalid-dict.exit | 1 + .../union-branch-invalid-dict.json | 4 + .../qapi-schema/union-branch-invalid-dict.out | 0 tests/qapi-schema/unknown-expr-key.err | 3 +- tests/qapi-schema/unknown-expr-key.json | 2 +- 81 files changed, 681 insertions(+), 266 deletions(-) create mode 100644 tests/qapi-schema/alternate-invalid-dict.err rename tests/qapi-schema/{enum-dict-member.exit => alternate-invalid-dict.exit} (100%) create mode 100644 tests/qapi-schema/alternate-invalid-dict.json rename tests/qapi-schema/{enum-dict-member.out => alternate-invalid-dict.out} (100%) create mode 100644 tests/qapi-schema/enum-bad-member.err create mode 100644 tests/qapi-schema/enum-bad-member.exit create mode 100644 tests/qapi-schema/enum-bad-member.json create mode 100644 tests/qapi-schema/enum-bad-member.out create mode 100644 tests/qapi-schema/enum-dict-member-unknown.err create mode 100644 tests/qapi-schema/enum-dict-member-unknown.exit create mode 100644 tests/qapi-schema/enum-dict-member-unknown.json create mode 100644 tests/qapi-schema/enum-dict-member-unknown.out delete mode 100644 tests/qapi-schema/enum-dict-member.err delete mode 100644 tests/qapi-schema/enum-dict-member.json 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/event-member-invalid-dict.err create mode 100644 tests/qapi-schema/event-member-invalid-dict.exit create mode 100644 tests/qapi-schema/event-member-invalid-dict.json create mode 100644 tests/qapi-schema/event-member-invalid-dict.out create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.err create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.exit create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.json create mode 100644 tests/qapi-schema/flat-union-inline-invalid-dict.out create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.err create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.exit create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.json create mode 100644 tests/qapi-schema/flat-union-invalid-if-discriminator.out create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.err create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.exit create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.json create mode 100644 tests/qapi-schema/nested-struct-data-invalid-dict.out create mode 100644 tests/qapi-schema/struct-member-invalid-dict.err create mode 100644 tests/qapi-schema/struct-member-invalid-dict.exit create mode 100644 tests/qapi-schema/struct-member-invalid-dict.json create mode 100644 tests/qapi-schema/struct-member-invalid-dict.out create mode 100644 tests/qapi-schema/union-branch-invalid-dict.err create mode 100644 tests/qapi-schema/union-branch-invalid-dict.exit create mode 100644 tests/qapi-schema/union-branch-invalid-dict.json create mode 100644 tests/qapi-schema/union-branch-invalid-dict.out--
2.20.0.rc1
PATCH v7 27/27 qapi: add conditions to REPLICATION type/commands on the schema
In reply to Marc-André LureauAdd #if defined(CONFIG_REPLICATION) in generated code, and adjust the code accordingly.
Made 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 closer to killing qmp_unregister_commands_hack().
* enum BlockdevDriver value "replication" in command blockdev-add
* BlockdevOptions variant @replication
and related structures.
--- qapi/block-core.json | 13 +++++++++---- qapi/migration.json | 12 ++++++++---- migration/colo.c | 16 ++++------------ monitor.c | 5 ----- 4 files changed, 21 insertions(+), 25 deletions(-)
qapi/block-core.json +9 -4
@@ -2623,7 +2623,9 @@ 'copy-on-read', 'dmg', 'file', 'ftp', 'ftps', 'gluster', 'host_cdrom', 'host_device', 'http', 'https', 'iscsi', 'luks', 'nbd', 'nfs', 'null-aio', 'null-co', 'nvme', 'parallels', 'qcow', - 'qcow2', 'qed', 'quorum', 'raw', 'rbd', 'replication', 'sheepdog', + 'qcow2', 'qed', 'quorum', 'raw', 'rbd', + { 'name': 'replication', 'if': 'defined(CONFIG_REPLICATION)' }, + 'sheepdog', 'ssh', 'throttle', 'vdi', 'vhdx', 'vmdk', 'vpc', 'vvfat', 'vxhs' ] } ##
@@ -3380,7 +3382,8 @@ # # Since: 2.9 ## -{ 'enum' : 'ReplicationMode', 'data' : [ 'primary', 'secondary' ] } +{ 'enum' : 'ReplicationMode', 'data' : [ 'primary', 'secondary' ], + 'if': 'defined(CONFIG_REPLICATION)' } ## # @BlockdevOptionsReplication:
@@ -3398,7 +3401,8 @@ { 'struct': 'BlockdevOptionsReplication', 'base': 'BlockdevOptionsGenericFormat', 'data': { 'mode': 'ReplicationMode', - '*top-id': 'str' } } + '*top-id': 'str' }, + 'if': 'defined(CONFIG_REPLICATION)' } ## # @NFSTransport:
@@ -3714,7 +3718,8 @@ 'quorum': 'BlockdevOptionsQuorum', 'raw': 'BlockdevOptionsRaw', 'rbd': 'BlockdevOptionsRbd', - 'replication':'BlockdevOptionsReplication', + 'replication': { 'type': 'BlockdevOptionsReplication', + 'if': 'defined(CONFIG_REPLICATION)' }, 'sheepdog': 'BlockdevOptionsSheepdog', 'ssh': 'BlockdevOptionsSsh', 'throttle': 'BlockdevOptionsThrottle',
qapi/migration.json +8 -4
@@ -1257,7 +1257,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:
@@ -1272,7 +1273,8 @@ # Since: 2.9 ## { 'struct': 'ReplicationStatus', - 'data': { 'error': 'bool', '*desc': 'str' } } + 'data': { 'error': 'bool', '*desc': 'str' }, + 'if': 'defined(CONFIG_REPLICATION)' } ## # @query-xen-replication-status:
@@ -1289,7 +1291,8 @@ # Since: 2.9 ## { 'command': 'query-xen-replication-status', - 'returns': 'ReplicationStatus' } + 'returns': 'ReplicationStatus', + 'if': 'defined(CONFIG_REPLICATION)' } ## # @xen-colo-do-checkpoint:
@@ -1305,7 +1308,8 @@ # # Since: 2.9 ## -{ 'command': 'xen-colo-do-checkpoint' } +{ 'command': 'xen-colo-do-checkpoint', + 'if': 'defined(CONFIG_REPLICATION)' } ## # @COLOStatus:
migration/colo.c +4 -12
@@ -24,7 +24,9 @@#include "trace.h" #include "qemu/error-report.h" #include "migration/failover.h" +#ifdef CONFIG_REPLICATION #include "replication.h" +#endif #include "net/colo-compare.h" #include "net/colo.h" #include "block/block.h"
@@ -201,11 +203,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 ;
@@ -224,14 +226,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 );
@@ -246,19 +244,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 COLOStatus *qmp_query_colo_status (Error **errp) {
monitor.c +0 -5
@@ -1146,11 +1146,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" );qmp_unregister_command (&qmp_commands,"query-sev" );
Signature · 1 line
2.20.0.rc1
Re: [Qemu-devel] [PATCH for-4.0 v7 27/27] qapi: add conditions to REPLICATION type/commands on the schema
Quoted 172 lines · Marc-André Lureau
Add #if
defined(CONFIG_REPLICATION)in generated code, and adjust the code accordingly.Made 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 closer to killing
qmp_unregister_commands_hack().* enum BlockdevDriver value "replication" in command blockdev-add
* BlockdevOptions variant @replication
and related structures.
Signed-off-by Marc-André Lureau <[email protected]>Reviewed-by Markus Armbruster <[email protected]>--- qapi/block-core.json | 13 +++++++++---- qapi/migration.json | 12 ++++++++---- migration/colo.c | 16 ++++------------ monitor.c | 5 ----- 4 files changed, 21 insertions(+), 25 deletions(-)qapi/block-core.json +9
-4@@ -2623,7 +2623,9 @@ 'copy-on-read', 'dmg', 'file', 'ftp', 'ftps', 'gluster', 'host_cdrom', 'host_device', 'http', 'https', 'iscsi', 'luks', 'nbd', 'nfs', 'null-aio', 'null-co', 'nvme', 'parallels', 'qcow', - 'qcow2', 'qed', 'quorum', 'raw', 'rbd', 'replication', 'sheepdog', + 'qcow2', 'qed', 'quorum', 'raw', 'rbd', + { 'name': 'replication', 'if': 'defined(CONFIG_REPLICATION)' }, + 'sheepdog', 'ssh', 'throttle', 'vdi', 'vhdx', 'vmdk', 'vpc', 'vvfat', 'vxhs' ] } ##@@ -3380,7 +3382,8 @@ # # Since: 2.9 ## -{ 'enum' : 'ReplicationMode', 'data' : [ 'primary', 'secondary' ] } +{ 'enum' : 'ReplicationMode', 'data' : [ 'primary', 'secondary' ], + 'if': 'defined(CONFIG_REPLICATION)' } ## # @BlockdevOptionsReplication:@@ -3398,7 +3401,8 @@ { 'struct': 'BlockdevOptionsReplication', 'base': 'BlockdevOptionsGenericFormat', 'data': { 'mode': 'ReplicationMode', - '*top-id': 'str' } } + '*top-id': 'str' }, + 'if': 'defined(CONFIG_REPLICATION)' } ## # @NFSTransport:@@ -3714,7 +3718,8 @@ 'quorum': 'BlockdevOptionsQuorum', 'raw': 'BlockdevOptionsRaw', 'rbd': 'BlockdevOptionsRbd', - 'replication':'BlockdevOptionsReplication', + 'replication': { 'type': 'BlockdevOptionsReplication', + 'if': 'defined(CONFIG_REPLICATION)' }, 'sheepdog': 'BlockdevOptionsSheepdog', 'ssh': 'BlockdevOptionsSsh', 'throttle': 'BlockdevOptionsThrottle',qapi/migration.json +8
-4@@ -1257,7 +1257,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:@@ -1272,7 +1273,8 @@ # Since: 2.9 ## { 'struct': 'ReplicationStatus', - 'data': { 'error': 'bool', '*desc': 'str' } } + 'data': { 'error': 'bool', '*desc': 'str' }, + 'if': 'defined(CONFIG_REPLICATION)' } ## # @query-xen-replication-status:@@ -1289,7 +1291,8 @@ # Since: 2.9 ## { 'command': 'query-xen-replication-status', - 'returns': 'ReplicationStatus' } + 'returns': 'ReplicationStatus', + 'if': 'defined(CONFIG_REPLICATION)' } ## # @xen-colo-do-checkpoint:@@ -1305,7 +1308,8 @@ # # Since: 2.9 ## -{ 'command': 'xen-colo-do-checkpoint' } +{ 'command': 'xen-colo-do-checkpoint', + 'if': 'defined(CONFIG_REPLICATION)' } ## # @COLOStatus:migration/colo.c +3
-8@@ -24,7 +24,9 @@#include "trace.h" #include "qemu/error-report.h" #include "migration/failover.h" +#ifdef CONFIG_REPLICATION #include "replication.h" +#endif #include "net/colo-compare.h" #include "net/colo.h" #include "block/block.h" @@ -201,11 +203,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 ;@@ -224,14 +226,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 );@@ -246,19 +244,13 @@ ReplicationStatus *qmp_query_xen_replication_status(Error **errp)error_free (err);return s; -#else -abort (); -#endif }
migration/colo.c ReplicationStatus *qmp_query_xen_replication_status(Error **errp) · line 253
Quoted 10 lines
migration/colo.c +1
-4void qmp_xen_colo_do_checkpoint (Error **errp) { -#ifdef CONFIG_REPLICATION replication_do_checkpoint_all (errp); -#else -abort (); -#endif } +#endif
/* CONFIG_REPLICATION */ after the endif wouldn't hurt.
Quoted 9 lines
migration/colo.c +0
-0COLOStatus *qmp_query_colo_status (Error **errp) {monitor.c +0
-0@@ -1146,11 +1146,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 1151
Quoted 10 lines
monitor.c +0
-5static 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" );qmp_unregister_command (&qmp_commands,"query-sev" );
Only TARGET conditionals left here. Good.
Re: [Qemu-devel] [PATCH for-4.0 v7 27/27] qapi: add conditions to REPLICATION type/commands on the schema
Hi
Quoted 201 lines · Markus Armbruster
Quoted 172 lines · Marc-André Lureau
Add #if
defined(CONFIG_REPLICATION)in generated code, and adjust the code accordingly.Made 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 closer to killing
qmp_unregister_commands_hack().* enum BlockdevDriver value "replication" in command blockdev-add
* BlockdevOptions variant @replication
and related structures.
Signed-off-by Marc-André Lureau <[email protected]>Reviewed-by Markus Armbruster <[email protected]>--- qapi/block-core.json | 13 +++++++++---- qapi/migration.json | 12 ++++++++---- migration/colo.c | 16 ++++------------ monitor.c | 5 ----- 4 files changed, 21 insertions(+), 25 deletions(-)qapi/block-core.json +9
-4@@ -2623,7 +2623,9 @@ 'copy-on-read', 'dmg', 'file', 'ftp', 'ftps', 'gluster', 'host_cdrom', 'host_device', 'http', 'https', 'iscsi', 'luks', 'nbd', 'nfs', 'null-aio', 'null-co', 'nvme', 'parallels', 'qcow', - 'qcow2', 'qed', 'quorum', 'raw', 'rbd', 'replication', 'sheepdog', + 'qcow2', 'qed', 'quorum', 'raw', 'rbd', + { 'name': 'replication', 'if': 'defined(CONFIG_REPLICATION)' }, + 'sheepdog', 'ssh', 'throttle', 'vdi', 'vhdx', 'vmdk', 'vpc', 'vvfat', 'vxhs' ] } ##@@ -3380,7 +3382,8 @@ # # Since: 2.9 ## -{ 'enum' : 'ReplicationMode', 'data' : [ 'primary', 'secondary' ] } +{ 'enum' : 'ReplicationMode', 'data' : [ 'primary', 'secondary' ], + 'if': 'defined(CONFIG_REPLICATION)' } ## # @BlockdevOptionsReplication:@@ -3398,7 +3401,8 @@ { 'struct': 'BlockdevOptionsReplication', 'base': 'BlockdevOptionsGenericFormat', 'data': { 'mode': 'ReplicationMode', - '*top-id': 'str' } } + '*top-id': 'str' }, + 'if': 'defined(CONFIG_REPLICATION)' } ## # @NFSTransport:@@ -3714,7 +3718,8 @@ 'quorum': 'BlockdevOptionsQuorum', 'raw': 'BlockdevOptionsRaw', 'rbd': 'BlockdevOptionsRbd', - 'replication':'BlockdevOptionsReplication', + 'replication': { 'type': 'BlockdevOptionsReplication', + 'if': 'defined(CONFIG_REPLICATION)' }, 'sheepdog': 'BlockdevOptionsSheepdog', 'ssh': 'BlockdevOptionsSsh', 'throttle': 'BlockdevOptionsThrottle',qapi/migration.json +8
-4@@ -1257,7 +1257,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:@@ -1272,7 +1273,8 @@ # Since: 2.9 ## { 'struct': 'ReplicationStatus', - 'data': { 'error': 'bool', '*desc': 'str' } } + 'data': { 'error': 'bool', '*desc': 'str' }, + 'if': 'defined(CONFIG_REPLICATION)' } ## # @query-xen-replication-status:@@ -1289,7 +1291,8 @@ # Since: 2.9 ## { 'command': 'query-xen-replication-status', - 'returns': 'ReplicationStatus' } + 'returns': 'ReplicationStatus', + 'if': 'defined(CONFIG_REPLICATION)' } ## # @xen-colo-do-checkpoint:@@ -1305,7 +1308,8 @@ # # Since: 2.9 ## -{ 'command': 'xen-colo-do-checkpoint' } +{ 'command': 'xen-colo-do-checkpoint', + 'if': 'defined(CONFIG_REPLICATION)' } ## # @COLOStatus:migration/colo.c +3
-8@@ -24,7 +24,9 @@#include "trace.h" #include "qemu/error-report.h" #include "migration/failover.h" +#ifdef CONFIG_REPLICATION #include "replication.h" +#endif #include "net/colo-compare.h" #include "net/colo.h" #include "block/block.h" @@ -201,11 +203,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 ;@@ -224,14 +226,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 );@@ -246,19 +244,13 @@ ReplicationStatus *qmp_query_xen_replication_status(Error **errp)error_free (err);return s; -#else -abort (); -#endif }migration/colo.c ReplicationStatus *qmp_query_xen_replication_status(Error **errp) · line 253
Quoted 10 lines
migration/colo.c +1
-4void qmp_xen_colo_do_checkpoint (Error **errp) { -#ifdef CONFIG_REPLICATION replication_do_checkpoint_all (errp); -#else -abort (); -#endif } +#endif /*
CONFIG_REPLICATION*/ after the endif wouldn't hurt.Quoted 13 lines
migration/colo.c +0
-0COLOStatus *qmp_query_colo_status (Error **errp) {monitor.c +0
-2@@ -1146,11 +1146,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" );
Quoted 8 lines
monitor.c static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data, · line 1151
Quoted 6 lines
monitor.c +0
-3-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" );qmp_unregister_command (&qmp_commands,"query-sev" );Only TARGET conditionals left here. Good.
IIRC, they are removed in part 3.
Quoted 2 lines
Reviewed-by Markus Armbruster <[email protected]>
Quoted 8 lines · Marc-André Lureau
This is the second part of the "add #if pre-processor conditions to generated code" series, adding schema member conditions (roughly 16-38/49).
Members can be exploded as dictionnary with 'type'/'if' keys:
{ 'struct': 'TestIfStruct', 'data':
{ 'foo': 'int',
Quoted 7 lines
'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)' } ] }
PATCH 03,18,20,25 are only losely related to this second part. They are also ready. Queued on branch qapi-next in my public repository, with PATCH 18 and 20 squashed together.
I took the liberty to implement the the idea on normalization I mentioned in review of PATCH 08. I also squashed a few patches that are essentially fixups into the patches they fix. I did not address any other review comments. Result is on branch qapi-if-2. Please have a look.
Hi
Quoted 23 lines · Markus Armbruster
Quoted 8 lines · Marc-André Lureau
This is the second part of the "add #if pre-processor conditions to generated code" series, adding schema member conditions (roughly 16-38/49).
Members can be exploded as dictionnary with 'type'/'if' keys:
{ 'struct': 'TestIfStruct', 'data':
{ 'foo': 'int',Quoted 7 lines
'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)' } ] }PATCH 03,18,20,25 are only losely related to this second part. They are also ready. Queued on branch qapi-next in my public repository, with PATCH 18 and 20 squashed together.
Quoted 6 lines
I took the liberty to implement the the idea on normalization I mentioned in review of PATCH 08. I also squashed a few patches that are essentially fixups into the patches they fix. I did not address any other review comments. Result is on branch qapi-if-2. Please have a look.
Looks good to me. Is there something left to rework from this branch or it is good to go?
thanks
Signature · 1 line
Marc-André Lureau
Quoted 33 lines · Marc-André Lureau
Hi
Quoted 23 lines · Markus Armbruster
Quoted 8 lines · Marc-André Lureau
This is the second part of the "add #if pre-processor conditions to generated code" series, adding schema member conditions (roughly 16-38/49).
Members can be exploded as dictionnary with 'type'/'if' keys:
{ 'struct': 'TestIfStruct', 'data':
{ 'foo': 'int',Quoted 7 lines
'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)' } ] }PATCH 03,18,20,25 are only losely related to this second part. They are also ready. Queued on branch qapi-next in my public repository, with PATCH 18 and 20 squashed together.
Quoted 6 lines
I took the liberty to implement the the idea on normalization I mentioned in review of PATCH 08. I also squashed a few patches that are essentially fixups into the patches they fix. I did not address any other review comments. Result is on branch qapi-if-2. Please have a look.
Quoted 3 lines
Looks good to me. Is there something left to rework from this branch or it is good to go?
thanks
I adressed only the one review comment that was hard to explain, plus some patch squashing. The other review comments still need to be considered and worked in.