file-posix: Add dynamic-auto-read-only QAPI feature
PATCH v2 1/6 qapi: Support features for structs
In reply to Kevin WolfSometimes, the behaviour of QEMU changes compatibly, but without a change in the QMP syntax (usually by allowing values or operations that previously resulted in an error). QMP clients may still need to know whether the extension is available.
This allows to add a list of features to struct definitions that will be made visible to QMP clients through schema introspection.
--- qapi/introspect.json | 8 +++- docs/devel/qapi-code-gen.txt | 38 ++++++++++++++++ scripts/qapi/common.py | 61 +++++++++++++++++++++----- scripts/qapi/doc.py | 3 +- scripts/qapi/introspect.py | 6 ++- scripts/qapi/types.py | 3 +- scripts/qapi/visit.py | 3 +- tests/qapi-schema/double-type.err | 2 +- tests/qapi-schema/test-qapi.py | 3 +- tests/qapi-schema/unknown-expr-key.err | 2 +- 10 files changed, 111 insertions(+), 18 deletions(-)
qapi/introspect.json +7 -1
@@ -174,6 +174,11 @@ # and may even differ from the order of the values of the # enum type of the @tag. # +# @features: names of features that are supported by this version and build and +# aren't othervise visible through schema introspection (e.g. change +# of behaviour related to an object type that didn't come with a +# syntactic change in the schema of the object type) (since: 4.1) +# # Values of this type are JSON object on the wire. # # Since: 2.5
@@ -181,7 +186,8 @@ { 'struct': 'SchemaInfoObject', 'data': { 'members': [ 'SchemaInfoObjectMember' ], '*tag': 'str', - '*variants': [ 'SchemaInfoObjectVariant' ] } } + '*variants': [ 'SchemaInfoObjectVariant' ], + '*features': [ 'str' ] } } ## # @SchemaInfoObjectMember:
docs/devel/qapi-code-gen.txt +38 -0
@@ -719,6 +719,34 @@ any non-empty complex type (struct, union, or alternate), and a pointer to that QAPI type is passed as a single argument. +=== Features === + +Sometimes, the behaviour of QEMU changes compatibly, but without a +change in the QMP syntax (usually by allowing values or operations that +previously resulted in an error). QMP clients may still need to know +whether the extension is available. + +For this purpose, a list of features can be specified for a struct type. +This is exposed to the client as a list of string, where each string +signals that this build of QEMU shows a certain behaviour. + +In the schema, features can be specified as simple strings, for example: + +{ 'struct': 'TestType', + 'data': { 'number': 'int' }, + 'features': [ 'allow-negative-numbers' ] } + +Another option is to specify features as dictionaries, where the key +'name' specifies the feature string to be exposed to clients: + +{ 'struct': 'TestType', + 'data': { 'number': 'int' }, + 'features': [ { 'name': 'allow-negative-numbers' } ] } + +This expanded form is necessary if you want to make the feature +conditional (see below in "Configuring the schema"). + + === Downstream extensions === QAPI schema names that are externally visible, say in the Client JSON
@@ -771,6 +799,16 @@ Example: a conditional 'bar' enum member. [ 'foo', { 'name' : 'bar', 'if': 'defined(IFCOND)' } ] } +Similarly, features can be specified as a dictionary with a 'name' and +an 'if' key. + +Example: a conditional 'allow-negative-numbers' feature + +{ 'struct': 'TestType', + 'data': { 'number': 'int' }, + 'features': [ { 'name': 'allow-negative-numbers', + '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.
scripts/qapi/common.py +51 -10
165 lines not on this page · raw message
scripts/qapi/doc.py +2 -1
@@ -220,7 +220,8 @@ class QAPISchemaGenDocVisitor(qapi.common.QAPISchemaVisitor): body=texi_entity(doc, 'Values', ifcond, member_func=texi_enum_value))) - def visit_object_type(self, name, info, ifcond, base, members, variants): + def visit_object_type(self, name, info, ifcond, base, members, variants, + features): doc = self.cur_doc if base and base.is_implicit(): base = None
scripts/qapi/introspect.py +5 -1
@@ -188,11 +188,15 @@ const QLitObject %(c_name)s = %(c_string)s; self._gen_qlit('[' + element + ']', 'array', {'element-type': element}, ifcond) - def visit_object_type_flat(self, name, info, ifcond, members, variants): + def visit_object_type_flat(self, name, info, ifcond, members, variants, + features): obj = {'members': [self._gen_member(m) for m in members]} if variants: obj.update(self._gen_variants(variants.tag_member.name, variants.variants)) + if features: + obj['features'] = [ (f.name, {'if': f.ifcond}) for f in features ] + self._gen_qlit(name, 'object', obj, ifcond) def visit_alternate_type(self, name, info, ifcond, variants):
scripts/qapi/types.py +2 -1
@@ -227,7 +227,8 @@ class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): self._genh.add(gen_array(name, element_type)) self._gen_type_cleanup(name) - def visit_object_type(self, name, info, ifcond, base, members, variants): + def visit_object_type(self, name, info, ifcond, base, members, variants, + features): # Nothing to do for the special empty builtin if name == 'q_empty': return
scripts/qapi/visit.py +2 -1
@@ -326,7 +326,8 @@ class QAPISchemaGenVisitVisitor(QAPISchemaModularCVisitor): self._genh.add(gen_visit_decl(name)) self._genc.add(gen_visit_list(name, element_type)) - def visit_object_type(self, name, info, ifcond, base, members, variants): + def visit_object_type(self, name, info, ifcond, base, members, variants, + features): # Nothing to do for the special empty builtin if name == 'q_empty': return
tests/qapi-schema/double-type.err +1 -1
@@ -1,2 +1,2 @@ tests/qapi-schema/double-type.json:2: Unknown key 'command' in struct 'bar' -Valid keys are 'base', 'data', 'if', 'struct'. +Valid keys are 'base', 'data', 'features', 'if', 'struct'.
tests/qapi-schema/test-qapi.py +2 -1
@@ -38,7 +38,8 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor): print('array %s %s' % (name, element_type.name)) self._print_if(ifcond) - def visit_object_type(self, name, info, ifcond, base, members, variants): + def visit_object_type(self, name, info, ifcond, base, members, variants, + features): print('object %s' % name) if base: print(' base %s' % base.name)
tests/qapi-schema/unknown-expr-key.err +1 -1
@@ -1,2 +1,2 @@ tests/qapi-schema/unknown-expr-key.json:2: Unknown keys 'bogus', 'phony' in struct 'bar' -Valid keys are 'base', 'data', 'if', 'struct'. +Valid keys are 'base', 'data', 'features', 'if', 'struct'.
Signature · 1 line
2.20.1
v2 cover file-posix: Add dynamic-auto-read-only QAPI feature
This series adds optional feature lists to struct definitions in the QAPI schema and makes use of them to advertise the new behaviour of auto-read-only=on in file-posix.
v2:
- Check that features have well-formed names instead of just checking that they are strings
- Use QAPISchemaFeature objects instead of dicts
- Catch duplicate feature names (and test that a feature name doesn't conflict with an object member)
- Added patch 4 and 5 to add rudimentary support for features to QAPIDoc so it doesn't complain about the documentation format suggested by Markus. Used that format in the documentation of dynamic-auto-read-only.
Kevin Wolf (6):
qapi: Support features for structs tests/qapi-schema: Test for good feature lists in structs tests/qapi-schema: Error case tests for features in structs
qapi: Disentangle QAPIDoc code
qapi: Allow documentation for features
file-posix: Add dynamic-auto-read-only QAPI feature
qapi/block-core.json | 13 +- qapi/introspect.json | 8 +- tests/qapi-schema/features-bad-type.json | 3 + .../qapi-schema/features-duplicate-name.json | 3 + tests/qapi-schema/features-missing-name.json | 3 + tests/qapi-schema/features-name-bad-type.json | 3 + tests/qapi-schema/features-no-list.json | 3 + tests/qapi-schema/features-unknown-key.json | 3 + tests/qapi-schema/qapi-schema-test.json | 30 +++ docs/devel/qapi-code-gen.txt | 38 ++++ scripts/qapi/common.py | 209 ++++++++++++++---- scripts/qapi/doc.py | 14 +- scripts/qapi/introspect.py | 6 +- scripts/qapi/types.py | 3 +- scripts/qapi/visit.py | 3 +- tests/Makefile.include | 6 + tests/qapi-schema/double-type.err | 2 +- tests/qapi-schema/features-bad-type.err | 1 + tests/qapi-schema/features-bad-type.exit | 1 + tests/qapi-schema/features-bad-type.out | 0 tests/qapi-schema/features-duplicate-name.err | 1 + .../qapi-schema/features-duplicate-name.exit | 1 + tests/qapi-schema/features-duplicate-name.out | 0 tests/qapi-schema/features-missing-name.err | 1 + tests/qapi-schema/features-missing-name.exit | 1 + tests/qapi-schema/features-missing-name.out | 0 tests/qapi-schema/features-name-bad-type.err | 1 + tests/qapi-schema/features-name-bad-type.exit | 1 + tests/qapi-schema/features-name-bad-type.out | 0 tests/qapi-schema/features-no-list.err | 1 + tests/qapi-schema/features-no-list.exit | 1 + tests/qapi-schema/features-no-list.out | 0 tests/qapi-schema/features-unknown-key.err | 2 + tests/qapi-schema/features-unknown-key.exit | 1 + tests/qapi-schema/features-unknown-key.out | 0 tests/qapi-schema/qapi-schema-test.out | 32 +++ tests/qapi-schema/test-qapi.py | 7 +- tests/qapi-schema/unknown-expr-key.err | 2 +- 38 files changed, 358 insertions(+), 46 deletions(-) create mode 100644 tests/qapi-schema/features-bad-type.json create mode 100644 tests/qapi-schema/features-duplicate-name.json create mode 100644 tests/qapi-schema/features-missing-name.json create mode 100644 tests/qapi-schema/features-name-bad-type.json create mode 100644 tests/qapi-schema/features-no-list.json create mode 100644 tests/qapi-schema/features-unknown-key.json create mode 100644 tests/qapi-schema/features-bad-type.err create mode 100644 tests/qapi-schema/features-bad-type.exit create mode 100644 tests/qapi-schema/features-bad-type.out create mode 100644 tests/qapi-schema/features-duplicate-name.err create mode 100644 tests/qapi-schema/features-duplicate-name.exit create mode 100644 tests/qapi-schema/features-duplicate-name.out create mode 100644 tests/qapi-schema/features-missing-name.err create mode 100644 tests/qapi-schema/features-missing-name.exit create mode 100644 tests/qapi-schema/features-missing-name.out create mode 100644 tests/qapi-schema/features-name-bad-type.err create mode 100644 tests/qapi-schema/features-name-bad-type.exit create mode 100644 tests/qapi-schema/features-name-bad-type.out create mode 100644 tests/qapi-schema/features-no-list.err create mode 100644 tests/qapi-schema/features-no-list.exit create mode 100644 tests/qapi-schema/features-no-list.out create mode 100644 tests/qapi-schema/features-unknown-key.err create mode 100644 tests/qapi-schema/features-unknown-key.exit create mode 100644 tests/qapi-schema/features-unknown-key.out
Signature · 1 line
2.20.1
PATCH v2 5/6 qapi: Allow documentation for features
Features will be documented in a new part introduced by a "Features:" line, after arguments and before named sections.
--- scripts/qapi/common.py | 43 ++++++++++++++++++++++++++++++++++++++---- scripts/qapi/doc.py | 11 +++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-)
scripts/qapi/common.py +39 -4
@@ -132,6 +132,9 @@ class QAPIDoc(object): ARGS means that we're parsing the arguments section. Any symbol name is interpreted as an argument and an ArgSection is created for it. + FEATURES means that we're parsing features sections. Any symbol name is + interpreted as a feature. + VARIOUS is the final part where freeform sections may appear. This includes named sections such as "Return:" as well as unnamed paragraphs. No symbols are allowed any more in this part.
@@ -139,7 +142,8 @@ class QAPIDoc(object): # Can't make it a subclass of Enum because of Python 2 BODY = 0 ARGS = 1 - VARIOUS = 2 + FEATURES = 2 + VARIOUS = 3 def __init__(self, parser, info): # self._parser is used to report errors with QAPIParseError. The
@@ -152,6 +156,7 @@ class QAPIDoc(object): self.body = QAPIDoc.Section() # dict mapping parameter name to ArgSection self.args = OrderedDict() + self.features = OrderedDict() # a list of Section self.sections = [] # the current section
@@ -180,6 +185,8 @@ class QAPIDoc(object): self._append_body_line(line) elif self._part == QAPIDoc.SymbolPart.ARGS: self._append_args_line(line) + elif self._part == QAPIDoc.SymbolPart.FEATURES: + self._append_features_line(line) elif self._part == QAPIDoc.SymbolPart.VARIOUS: self._append_various_line(line) else:
@@ -215,6 +222,8 @@ class QAPIDoc(object): if name.startswith('@') and name.endswith(':'): self._part = QAPIDoc.SymbolPart.ARGS self._append_args_line(line) + elif line == 'Features:': + self._part = QAPIDoc.SymbolPart.FEATURES elif self.symbol and self._check_named_section(line, name): self._append_various_line(line) else:
@@ -231,6 +240,26 @@ class QAPIDoc(object): self._start_args_section(name[1:-1]) elif self._check_named_section(line, name): return self._append_various_line(line) + elif (self._section.text.endswith('\n\n') + and line and not line[0].isspace()): + if line == 'Features:': + self._part = QAPIDoc.SymbolPart.FEATURES + return + else: + self._start_section() + self._part = QAPIDoc.SymbolPart.VARIOUS + return self._append_various_line(line) + + self._append_freeform(line.strip()) + + def _append_features_line(self, line): + name = line.split(' ', 1)[0] + + if name.startswith('@') and name.endswith(':'): + line = line[len(name)+1:] + self._start_features_section(name[1:-1]) + elif self._check_named_section(line, name): + return self._append_various_line(line) elif (self._section.text.endswith('\n\n') and line and not line[0].isspace()): self._start_section()
@@ -256,17 +285,23 @@ class QAPIDoc(object): self._append_freeform(line) - def _start_args_section(self, name): + def _start_symbol_section(self, symbols_dict, name): # FIXME invalid names other than the empty string aren't flagged if not name: raise QAPIParseError(self._parser, "Invalid parameter name") - if name in self.args: + if name in symbols_dict: raise QAPIParseError(self._parser, "'%s' parameter name duplicated" % name) assert not self.sections self._end_section() self._section = QAPIDoc.ArgSection(name) - self.args[name] = self._section + symbols_dict[name] = self._section + + def _start_args_section(self, name): + self._start_symbol_section(self.args, name) + + def _start_features_section(self, name): + self._start_symbol_section(self.features, name) def _start_section(self, name=None): if name in ('Returns', 'Since') and self.has_section(name):
scripts/qapi/doc.py +11 -0
@@ -181,6 +181,16 @@ def texi_members(doc, what, base, variants, member_func): return '' return '\n@b{%s:}\n@table @asis\n%s@end table\n' % (what, items) +def texi_features(doc): + """Format the table of features""" + items = '' + for section in doc.features.values(): + desc = texi_format(section.text) + items += '@item @code{%s}\n%s' % (section.name, desc) + if not items: + return '' + return '\n@b{Features:}\n@table @asis\n%s@end table\n' % (items) + def texi_sections(doc, ifcond): """Format additional sections following arguments"""
@@ -201,6 +211,7 @@ def texi_entity(doc, what, ifcond, base=None, variants=None, member_func=texi_member): return (texi_body(doc) + texi_members(doc, what, base, variants, member_func) + + texi_features(doc) + texi_sections(doc, ifcond))
Signature · 1 line
2.20.1
PATCH v2 2/6 tests/qapi-schema: Test for good feature lists in structs
In reply to Kevin Wolf--- tests/qapi-schema/qapi-schema-test.json | 30 +++++++++++++++++++++++ tests/qapi-schema/qapi-schema-test.out | 32 +++++++++++++++++++++++++ tests/qapi-schema/test-qapi.py | 4 ++++ 3 files changed, 66 insertions(+)
tests/qapi-schema/qapi-schema-test.json +30 -0
@@ -242,3 +242,33 @@ { 'foo': 'TestIfStruct', 'bar': { 'type': ['TestIfEnum'], 'if': 'defined(TEST_IF_EVT_BAR)' } }, 'if': 'defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)' } + +# test 'features' for structs + +{ 'struct': 'FeatureStruct0', + 'data': { 'foo': 'int' }, + 'features': [] } +{ 'struct': 'FeatureStruct1', + 'data': { 'foo': 'int' }, + 'features': [ 'feature1' ] } +{ 'struct': 'FeatureStruct2', + 'data': { 'foo': 'int' }, + 'features': [ { 'name': 'feature1' } ] } +{ 'struct': 'FeatureStruct3', + 'data': { 'foo': 'int' }, + 'features': [ 'feature1', 'feature2' ] } +{ 'struct': 'FeatureStruct4', + 'data': { 'namespace-test': 'int' }, + 'features': [ 'namespace-test', 'int', 'name', 'if' ] } + +{ 'struct': 'CondFeatureStruct1', + 'data': { 'foo': 'int' }, + 'features': [ { 'name': 'feature1', 'if': 'defined(TEST_IF_FEATURE_1)'} ] } +{ 'struct': 'CondFeatureStruct2', + 'data': { 'foo': 'int' }, + 'features': [ { 'name': 'feature1', 'if': 'defined(TEST_IF_FEATURE_1)'}, + { 'name': 'feature2', 'if': 'defined(TEST_IF_FEATURE_2)'} ] } +{ 'struct': 'CondFeatureStruct3', + 'data': { 'foo': 'int' }, + 'features': [ { 'name': 'feature1', 'if': [ 'defined(TEST_IF_COND_1)', + 'defined(TEST_IF_COND_2)'] } ] }
tests/qapi-schema/qapi-schema-test.out +32 -0
@@ -354,3 +354,35 @@ object q_obj_TestIfEvent-arg event TestIfEvent q_obj_TestIfEvent-arg boxed=False if ['defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)'] +object FeatureStruct0 + member foo: int optional=False +object FeatureStruct1 + member foo: int optional=False + feature feature1 +object FeatureStruct2 + member foo: int optional=False + feature feature1 +object FeatureStruct3 + member foo: int optional=False + feature feature1 + feature feature2 +object FeatureStruct4 + member namespace-test: int optional=False + feature namespace-test + feature int + feature name + feature if +object CondFeatureStruct1 + member foo: int optional=False + feature feature1 + if ['defined(TEST_IF_FEATURE_1)'] +object CondFeatureStruct2 + member foo: int optional=False + feature feature1 + if ['defined(TEST_IF_FEATURE_1)'] + feature feature2 + if ['defined(TEST_IF_FEATURE_2)'] +object CondFeatureStruct3 + member foo: int optional=False + feature feature1 + if ['defined(TEST_IF_COND_1)', 'defined(TEST_IF_COND_2)']
tests/qapi-schema/test-qapi.py +4 -0
@@ -49,6 +49,10 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor): self._print_if(m.ifcond, 8) self._print_variants(variants) self._print_if(ifcond) + if features: + for f in features: + print(' feature %s' % f.name) + self._print_if(f.ifcond, 8) def visit_alternate_type(self, name, info, ifcond, variants): print('alternate %s' % name)
Signature · 1 line
2.20.1
PATCH v2 4/6 qapi: Disentangle QAPIDoc code
In reply to Kevin WolfDocumentation comment follow a certain structure: First, we have a text with a general description (called QAPIDoc.body). After this, descriptions of the arguments follow. Finally, we have part that contains various named sections.
The code doesn't show this structure but just checks the right side conditions so it happens to do the right set of things in the right phase. This is hard to follow, and adding support for documentation of features would be even harder.
This restructures the code so that the three parts are clearly separated. The code becomes a bit longer, but easier to follow.
--- scripts/qapi/common.py | 107 ++++++++++++++++++++++++++++++++--------- 1 file changed, 83 insertions(+), 24 deletions(-)
scripts/qapi/common.py +83 -24
@@ -120,6 +120,27 @@ class QAPIDoc(object): def connect(self, member): self.member = member + class SymbolPart: + """ + Describes which part of the documentation we're parsing right now. + + BODY means that we're ready to process freeform text into self.body. A + symbol name is only allowed if no other text was parsed yet. It is + interpreted as the symbol name that describes the currently documented + object. On getting the second symbol name, we proceed to ARGS. + + ARGS means that we're parsing the arguments section. Any symbol name is + interpreted as an argument and an ArgSection is created for it. + + VARIOUS is the final part where freeform sections may appear. This + includes named sections such as "Return:" as well as unnamed + paragraphs. No symbols are allowed any more in this part. + """ + # Can't make it a subclass of Enum because of Python 2 + BODY = 0 + ARGS = 1 + VARIOUS = 2 + def __init__(self, parser, info): # self._parser is used to report errors with QAPIParseError. The # resulting error position depends on the state of the parser.
@@ -135,6 +156,7 @@ class QAPIDoc(object): self.sections = [] # the current section self._section = self.body + self._part = QAPIDoc.SymbolPart.BODY def has_section(self, name): """Return True if we have a section with this name."""
@@ -154,37 +176,84 @@ class QAPIDoc(object): raise QAPIParseError(self._parser, "Missing space after #") line = line[1:] + if self._part == QAPIDoc.SymbolPart.BODY: + self._append_body_line(line) + elif self._part == QAPIDoc.SymbolPart.ARGS: + self._append_args_line(line) + elif self._part == QAPIDoc.SymbolPart.VARIOUS: + self._append_various_line(line) + else: + assert False + + + def end_comment(self): + self._end_section() + + def _check_named_section(self, line, name): + if name in ('Returns:', 'Since:', + # those are often singular or plural + 'Note:', 'Notes:', + 'Example:', 'Examples:', + 'TODO:'): + self._part = QAPIDoc.SymbolPart.VARIOUS + return True + return False + + def _append_body_line(self, line): + name = line.split(' ', 1)[0] # FIXME not nice: things like '# @foo:' and '# @foo: ' aren't # recognized, and get silently treated as ordinary text - if self.symbol: - self._append_symbol_line(line) - elif not self.body.text and line.startswith('@'): + if not self.symbol and not self.body.text and line.startswith('@'): if not line.endswith(':'): raise QAPIParseError(self._parser, "Line should end with :") self.symbol = line[1:-1] # FIXME invalid names other than the empty string aren't flagged if not self.symbol: raise QAPIParseError(self._parser, "Invalid name") + elif self.symbol: + # We already know that we document some symbol + if name.startswith('@') and name.endswith(':'): + self._part = QAPIDoc.SymbolPart.ARGS + self._append_args_line(line) + elif self.symbol and self._check_named_section(line, name): + self._append_various_line(line) + else: + self._append_freeform(line.strip()) else: - self._append_freeform(line) - - def end_comment(self): - self._end_section() + # This is free-form documentation without a symbol + self._append_freeform(line.strip()) - def _append_symbol_line(self, line): + def _append_args_line(self, line): name = line.split(' ', 1)[0] if name.startswith('@') and name.endswith(':'): line = line[len(name)+1:] self._start_args_section(name[1:-1]) - elif name in ('Returns:', 'Since:', - # those are often singular or plural - 'Note:', 'Notes:', - 'Example:', 'Examples:', - 'TODO:'): + elif self._check_named_section(line, name): + return self._append_various_line(line) + elif (self._section.text.endswith('\n\n') + and line and not line[0].isspace()): + self._start_section() + self._part = QAPIDoc.SymbolPart.VARIOUS + return self._append_various_line(line) + + self._append_freeform(line.strip()) + + def _append_various_line(self, line): + name = line.split(' ', 1)[0] + + if name.startswith('@') and name.endswith(':'): + raise QAPIParseError(self._parser, + "'%s' can't follow '%s' section" + % (name, self.sections[0].name)) + elif self._check_named_section(line, name): line = line[len(name)+1:] self._start_section(name[:-1]) + if (not self._section.name or + not self._section.name.startswith('Example')): + line = line.strip() + self._append_freeform(line) def _start_args_section(self, name):
@@ -194,10 +263,7 @@ class QAPIDoc(object): if name in self.args: raise QAPIParseError(self._parser, "'%s' parameter name duplicated" % name) - if self.sections: - raise QAPIParseError(self._parser, - "'@%s:' can't follow '%s' section" - % (name, self.sections[0].name)) + assert not self.sections self._end_section() self._section = QAPIDoc.ArgSection(name) self.args[name] = self._section
@@ -219,13 +285,6 @@ class QAPIDoc(object): self._section = None def _append_freeform(self, line): - in_arg = isinstance(self._section, QAPIDoc.ArgSection) - if (in_arg and self._section.text.endswith('\n\n') - and line and not line[0].isspace()): - self._start_section() - if (in_arg or not self._section.name - or not self._section.name.startswith('Example')): - line = line.strip() match = re.match(r'(@\S+:)', line) if match: raise QAPIParseError(self._parser,
Signature · 1 line
2.20.1
PATCH v2 3/6 tests/qapi-schema: Error case tests for features in structs
In reply to Kevin Wolf--- tests/qapi-schema/features-bad-type.json | 3 +++ tests/qapi-schema/features-duplicate-name.json | 3 +++ tests/qapi-schema/features-missing-name.json | 3 +++ tests/qapi-schema/features-name-bad-type.json | 3 +++ tests/qapi-schema/features-no-list.json | 3 +++ tests/qapi-schema/features-unknown-key.json | 3 +++ tests/Makefile.include | 6 ++++++ tests/qapi-schema/features-bad-type.err | 1 + tests/qapi-schema/features-bad-type.exit | 1 + tests/qapi-schema/features-bad-type.out | 0 tests/qapi-schema/features-duplicate-name.err | 1 + tests/qapi-schema/features-duplicate-name.exit | 1 + tests/qapi-schema/features-duplicate-name.out | 0 tests/qapi-schema/features-missing-name.err | 1 + tests/qapi-schema/features-missing-name.exit | 1 + tests/qapi-schema/features-missing-name.out | 0 tests/qapi-schema/features-name-bad-type.err | 1 + tests/qapi-schema/features-name-bad-type.exit | 1 + tests/qapi-schema/features-name-bad-type.out | 0 tests/qapi-schema/features-no-list.err | 1 + tests/qapi-schema/features-no-list.exit | 1 + tests/qapi-schema/features-no-list.out | 0 tests/qapi-schema/features-unknown-key.err | 2 ++ tests/qapi-schema/features-unknown-key.exit | 1 + tests/qapi-schema/features-unknown-key.out | 0 25 files changed, 37 insertions(+) create mode 100644 tests/qapi-schema/features-bad-type.json create mode 100644 tests/qapi-schema/features-duplicate-name.json create mode 100644 tests/qapi-schema/features-missing-name.json create mode 100644 tests/qapi-schema/features-name-bad-type.json create mode 100644 tests/qapi-schema/features-no-list.json create mode 100644 tests/qapi-schema/features-unknown-key.json create mode 100644 tests/qapi-schema/features-bad-type.err create mode 100644 tests/qapi-schema/features-bad-type.exit create mode 100644 tests/qapi-schema/features-bad-type.out create mode 100644 tests/qapi-schema/features-duplicate-name.err create mode 100644 tests/qapi-schema/features-duplicate-name.exit create mode 100644 tests/qapi-schema/features-duplicate-name.out create mode 100644 tests/qapi-schema/features-missing-name.err create mode 100644 tests/qapi-schema/features-missing-name.exit create mode 100644 tests/qapi-schema/features-missing-name.out create mode 100644 tests/qapi-schema/features-name-bad-type.err create mode 100644 tests/qapi-schema/features-name-bad-type.exit create mode 100644 tests/qapi-schema/features-name-bad-type.out create mode 100644 tests/qapi-schema/features-no-list.err create mode 100644 tests/qapi-schema/features-no-list.exit create mode 100644 tests/qapi-schema/features-no-list.out create mode 100644 tests/qapi-schema/features-unknown-key.err create mode 100644 tests/qapi-schema/features-unknown-key.exit create mode 100644 tests/qapi-schema/features-unknown-key.out
tests/qapi-schema/features-bad-type.json new file +3 -0
@@ -0,0 +1,3 @@ +{ 'struct': 'FeatureStruct0', + 'data': { 'foo': 'int' }, + 'features': [ [ 'a feature cannot be an array' ] ] }
tests/qapi-schema/features-duplicate-name.json new file +3 -0
@@ -0,0 +1,3 @@ +{ 'struct': 'FeatureStruct0', + 'data': { 'foo': 'int' }, + 'features': [ 'foo', 'bar', 'foo' ] }
tests/qapi-schema/features-missing-name.json new file +3 -0
@@ -0,0 +1,3 @@ +{ 'struct': 'FeatureStruct0', + 'data': { 'foo': 'int' }, + 'features': [ { 'if': 'defined(NAMELESS_FEATURES)' } ] }
tests/qapi-schema/features-name-bad-type.json new file +3 -0
@@ -0,0 +1,3 @@ +{ 'struct': 'FeatureStruct0', + 'data': { 'foo': 'int' }, + 'features': [ { 'name': { 'feature-type': 'object' } } ] }
tests/qapi-schema/features-no-list.json new file +3 -0
@@ -0,0 +1,3 @@ +{ 'struct': 'FeatureStruct0', + 'data': { 'foo': 'int' }, + 'features': 'bar' }
tests/qapi-schema/features-unknown-key.json new file +3 -0
@@ -0,0 +1,3 @@ +{ 'struct': 'FeatureStruct0', + 'data': { 'foo': 'int' }, + 'features': [ { 'name': 'bar', 'colour': 'red' } ] }
tests/Makefile.include +6 -0
@@ -377,6 +377,12 @@ 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 += features-bad-type.json +qapi-schema += features-duplicate-name.json +qapi-schema += features-missing-name.json +qapi-schema += features-name-bad-type.json +qapi-schema += features-no-list.json +qapi-schema += features-unknown-key.json qapi-schema += flat-union-array-branch.json qapi-schema += flat-union-bad-base.json qapi-schema += flat-union-bad-discriminator.json
tests/qapi-schema/features-bad-type.err new file +1 -0
@@ -0,0 +1 @@ +tests/qapi-schema/features-bad-type.json:1: Feature of struct FeatureStruct0 requires a string name
tests/qapi-schema/features-bad-type.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/features-bad-type.out new file +0 -0
tests/qapi-schema/features-duplicate-name.err new file +1 -0
@@ -0,0 +1 @@ +tests/qapi-schema/features-duplicate-name.json:1: 'foo' (feature of FeatureStruct0) collides with 'foo' (feature of FeatureStruct0)
tests/qapi-schema/features-duplicate-name.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/features-duplicate-name.out new file +0 -0
tests/qapi-schema/features-missing-name.err new file +1 -0
@@ -0,0 +1 @@ +tests/qapi-schema/features-missing-name.json:1: Key 'name' is missing from feature of struct FeatureStruct0
tests/qapi-schema/features-missing-name.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/features-missing-name.out new file +0 -0
tests/qapi-schema/features-name-bad-type.err new file +1 -0
@@ -0,0 +1 @@ +tests/qapi-schema/features-name-bad-type.json:1: Feature of struct FeatureStruct0 requires a string name
tests/qapi-schema/features-name-bad-type.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/features-name-bad-type.out new file +0 -0
tests/qapi-schema/features-no-list.err new file +1 -0
@@ -0,0 +1 @@ +tests/qapi-schema/features-no-list.json:1: Struct 'FeatureStruct0' requires an array for 'features'
tests/qapi-schema/features-no-list.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/features-no-list.out new file +0 -0
tests/qapi-schema/features-unknown-key.err new file +2 -0
@@ -0,0 +1,2 @@ +tests/qapi-schema/features-unknown-key.json:1: Unknown key 'colour' in feature of struct FeatureStruct0 +Valid keys are 'if', 'name'.
tests/qapi-schema/features-unknown-key.exit new file +1 -0
@@ -0,0 +1 @@ +1
tests/qapi-schema/features-unknown-key.out new file +0 -0
Signature · 1 line
2.20.1
PATCH v2 6/6 file-posix: Add dynamic-auto-read-only QAPI feature
In reply to Kevin WolfIn commit 23dece19da4 ('file-posix: Make auto-read-only dynamic') , auto-read-only=on changed its behaviour in file-posix for the 4.0 release. This change cannot be detected through the usual mechanisms like schema introspection. Add a new feature flag to the schema to allow libvirt to detect the presence of the new behaviour.
--- qapi/block-core.json | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
qapi/block-core.json +12 -1
@@ -2843,6 +2843,15 @@ # file is large, do not use in production. # (default: off) (since: 3.0) # +# Features: +# @dynamic-auto-read-only: If present, enabled auto-read-only means that the +# driver will open the image read-only at first, +# dynamically reopen the image file read-write when +# the first writer is attached to the node and reopen +# read-only when the last writer is detached. This +# allows to give QEMU write permissions only on demand +# when an operation actually needs write access. +# # Since: 2.9 ## { 'struct': 'BlockdevOptionsFile',
@@ -2852,7 +2861,9 @@ '*aio': 'BlockdevAioOptions', '*drop-cache': {'type': 'bool', 'if': 'defined(CONFIG_LINUX)'}, - '*x-check-cache-dropped': 'bool' } } + '*x-check-cache-dropped': 'bool' }, + 'features': [ { 'name': 'dynamic-auto-read-only', + 'if': 'defined(CONFIG_POSIX)' } ] } ## # @BlockdevOptionsNull:
Signature · 1 line
2.20.1
Re: [Qemu-devel] [PATCH v2 1/6] qapi: Support features for structs
In reply to Kevin Wolf"Support features for structs" accidentally fits the "Do stuff" commit message anti-pattern. Suggest:
qapi: Add feature flags to struct types
Quoted 6 lines · Kevin Wolf
Sometimes, the behaviour of QEMU changes compatibly, but without a change in the QMP syntax (usually by allowing values or operations that previously resulted in an error). QMP clients may still need to know whether the extension is available.
This allows to add a list of features to struct definitions that will be
What's "this"? Do you mean "This patch allows"?
Quoted 1 line
made visible to QMP clients through schema introspection.
Attentive readers might ask why only structs. The answer is of course that structs suffice for your immediate needs, and you chose to keep this patch simple by limiting it to what you actually need.
Now let me try to work that into your commit message:
qapi: Add feature flags to struct types
Sometimes, the behaviour of QEMU changes without a change in the QMP syntax (usually by allowing values or operations that previously resulted in an error). QMP clients may still need to know whether they can rely on the changed behavior.
To let us make such changes visible in introspection, add feature flags to the QAPI schema language. Looks like this:
{ 'struct': 'TestType',
'data': { 'number': 'int' }, 'features': [ 'allow-negative-numbers' ] }
Introspection then looks like
{ "name": "TestType", "meta-type": "object",
"members": [
{ "name": "number", "type": "int" } ],
"features": [ "allow-negative-numbers" ] }
This patch implements feature flags just for struct types. We'll implement them more widely as needed.
Quoted 20 lines
Signed-off-by Kevin Wolf <[email protected]>--- qapi/introspect.json | 8 +++- docs/devel/qapi-code-gen.txt | 38 ++++++++++++++++ scripts/qapi/common.py | 61 +++++++++++++++++++++----- scripts/qapi/doc.py | 3 +- scripts/qapi/introspect.py | 6 ++- scripts/qapi/types.py | 3 +- scripts/qapi/visit.py | 3 +- tests/qapi-schema/double-type.err | 2 +- tests/qapi-schema/test-qapi.py | 3 +- tests/qapi-schema/unknown-expr-key.err | 2 +- 10 files changed, 111 insertions(+), 18 deletions(-)qapi/introspect.json +0
-0
qapi/introspect.json line 181
Quoted 8 lines
qapi/introspect.json +5
-0@@ -174,6 +174,11 @@ # and may even differ from the order of the values of the # enum type of the @tag. # +# @features: names of features that are supported by this version and build and +# aren't othervise visible through schema introspection (e.g. change +# of behaviour related to an object type that didn't come with a +# syntactic change in the schema of the object type) (since: 4.1) +#
Specify "in no particular order", like we do elsewhere.
Let's wrap lines a little earlier.
I'm not sure this is the best place to explain the indented purpose of QAPI feature flags. Other SchemaInfoFOO will acquire @features. I feel each instance should carry just terse specification. Anything beyond that should be in just one place. Right now, query-qmp-schema looks like the best fit. We could instead write an introduction to introspection as a file comment, right under "# = QMP introspection".
Quoted 59 lines
qapi/introspect.json +2
-1# Values of this type are JSON object on the wire. # # Since: 2.5@@ -181,7 +186,8 @@ { 'struct': 'SchemaInfoObject', 'data': { 'members': [ 'SchemaInfoObjectMember' ], '*tag': 'str', - '*variants': [ 'SchemaInfoObjectVariant' ] } } + '*variants': [ 'SchemaInfoObjectVariant' ], + '*features': [ 'str' ] } } ## # @SchemaInfoObjectMember:docs/devel/qapi-code-gen.txt +31
-0@@ -719,6 +719,34 @@ any non-empty complex type (struct, union, or alternate), and a pointer to that QAPI type is passed as a single argument. +=== Features === + +Sometimes, the behaviour of QEMU changes compatibly, but without a +change in the QMP syntax (usually by allowing values or operations that +previously resulted in an error). QMP clients may still need to know +whether the extension is available. + +For this purpose, a list of features can be specified for a struct type. +This is exposed to the client as a list of string, where each string +signals that this build of QEMU shows a certain behaviour. + +In the schema, features can be specified as simple strings, for example: + +{ 'struct': 'TestType', + 'data': { 'number': 'int' }, + 'features': [ 'allow-negative-numbers' ] } + +Another option is to specify features as dictionaries, where the key +'name' specifies the feature string to be exposed to clients: + +{ 'struct': 'TestType', + 'data': { 'number': 'int' }, + 'features': [ { 'name': 'allow-negative-numbers' } ] } + +This expanded form is necessary if you want to make the feature +conditional (see below in "Configuring the schema"). + + === Downstream extensions === QAPI schema names that are externally visible, say in the Client JSON@@ -771,6 +799,16 @@ Example: a conditional 'bar' enum member. [ 'foo', { 'name' : 'bar', 'if': 'defined(IFCOND)' } ] } +Similarly, features can be specified as a dictionary with a 'name' and +an 'if' key. +
docs/devel/qapi-code-gen.txt Example: a conditional 'bar' enum member. · line 814
Quoted 10 lines
docs/devel/qapi-code-gen.txt +7
-0+Example: a conditional 'allow-negative-numbers' feature + +{ 'struct': 'TestType', + 'data': { 'number': 'int' }, + 'features': [ { 'name': 'allow-negative-numbers', + '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.
Feels bolted on, just like conditionals. I'm afraid we'll have to rework this document to arrest its deterioration.
I'm not asking you to do this work. Let's move on with this patch.
Quoted 13 lines
scripts/qapi/common.py +1
-0@@ -886,12 +886,25 @@ def check_enum(expr, info): def check_struct(expr, info): name = expr['struct'] members = expr['data'] + features = expr.get('features') check_type(info, "'data' for struct '%s'" % name, members, allow_dict=True, allow_optional=True) check_type(info, "'base' for struct '%s'" % name, expr.get('base'),
scripts/qapi/common.py def check_enum(expr, info): · line 903
Quoted 10 lines
scripts/qapi/common.py +8
-0allow_metas=['struct']) + if features: + if not isinstance(features, list): + raise QAPISemError(info, + "Struct '%s' requires an array for 'features'" % + name) + for f in features: + assert isinstance(f, dict) + check_known_keys(info, "feature of struct %s" % (name), f,
Redundant parenthesis: (name)
scripts/qapi/common.py def check_enum(expr, info): · line 907
Quoted 4 lines
scripts/qapi/common.py +4
-0+ ['name'], ['if']) + + check_if(f, info) + check_name(info, "Feature of struct %s" % (name), f['name'])
Again.
You managed to reduce the differences to check_enum() nicely since v1, paving the way for future de-duplication. I'm not asking you to de-duplicate now.
pycodestyle-3 complains about whitespace here, and in a few other places. Please use it to check your patches for avoidable style nits.
scripts/qapi/common.py def normalize_members(members): · line 966
Quoted 11 lines
scripts/qapi/common.py +4
-0def check_known_keys(info, source, keys, required, optional):@@ -947,6 +960,10 @@ def normalize_members(members): continue members[key] = {'type': arg} +def normalize_features(features): + if isinstance(features, list): + features[:] = [f if isinstance(f, dict) else {'name': f} + for f in features]
Duplicates the meat of normalize_enum(). I'm not asking you to de-duplicate now.
Quoted 45 lines
scripts/qapi/common.py +10
-4def check_exprs(exprs): global all_names@@ -986,8 +1003,10 @@ def check_exprs(exprs): normalize_members(expr['data']) elif 'struct' in expr: meta = 'struct' - check_keys(expr_elem, 'struct', ['data'], ['base', 'if']) + check_keys(expr_elem, 'struct', ['data'], + ['base', 'if', 'features']) normalize_members(expr['data']) + normalize_features(expr.get('features')) struct_types[expr[meta]] = expr elif 'command' in expr: meta = 'command'@@ -1126,10 +1145,12 @@ class QAPISchemaVisitor(object): def visit_array_type(self, name, info, ifcond, element_type): pass - def visit_object_type(self, name, info, ifcond, base, members, variants): + def visit_object_type(self, name, info, ifcond, base, members, variants, + features): pass - def visit_object_type_flat(self, name, info, ifcond, members, variants): + def visit_object_type_flat(self, name, info, ifcond, members, variants, + features): pass def visit_alternate_type(self, name, info, ifcond, variants):@@ -1290,7 +1311,7 @@ class QAPISchemaArrayType(QAPISchemaType): class QAPISchemaObjectType(QAPISchemaType): def __init__(self, name, info, doc, ifcond, - base, local_members, variants): + base, local_members, variants, features): # struct has local_members, optional base, and no variants # flat union has base, variants, and no local_members # simple union has local_members, variants, and no base@@ -1302,11 +1323,15 @@ class QAPISchemaObjectType(QAPISchemaType): if variants is not None: assert isinstance(variants, QAPISchemaObjectTypeVariants) variants.set_owner(name) + for f in features: + assert isinstance(f, QAPISchemaFeature)
scripts/qapi/common.py class QAPISchemaObjectType(QAPISchemaType): · line 1337
Quoted 10 lines
scripts/qapi/common.py +2
-0+ f.set_owner(name) self._base_name = base self.base = None self.local_members = local_members self.variants = variants self.members = None + self.features = features def check(self, schema): QAPISchemaType.check(self, schema)
if self.members is False: # check for cycles
raise QAPISemError(self.info,
"Object %s contains itself" % self.name)
if self.members:
return
self.members = False # mark as being checked
seen = OrderedDict()
if self._base_name:
self.base = schema.lookup_type(self._base_name) assert isinstance(self.base, QAPISchemaObjectType) self.base.check(schema) self.base.check_clash(self.info, seen)
for m in self.local_members:
m.check(schema)
m.check_clash(self.info, seen)
if self.doc:
self.doc.connect_member(m)
self.members = seen.values()
if self.variants:
scripts/qapi/common.py class QAPISchemaObjectType(QAPISchemaType): · line 1361
Quoted 6 lines
scripts/qapi/common.py +2
-0@@ -1332,6 +1357,12 @@ class QAPISchemaObjectType(QAPISchemaType): self.variants.check(schema, seen) assert self.variants.tag_member in self.members self.variants.check_clash(self.info, seen) + + # Features are in a namespace separate from members
name space
scripts/qapi/common.py class QAPISchemaObjectType(QAPISchemaType): · line 1362
Quoted 1 line
scripts/qapi/common.py +1
-0+ seen = OrderedDict()
seen = {} suffices. It doesn't for members (above), because there, we want seen.values() preserve order. See commit 23a4b2c6f19.
Fun fact: dict is guaranteed to preserve order as of Python 3.7.
Quoted 62 lines
scripts/qapi/common.py +15
-4+ for f in self.features: + f.check_clash(self.info, seen) + if self.doc: self.doc.check()@@ -1368,12 +1399,15 @@ class QAPISchemaObjectType(QAPISchemaType): def visit(self, visitor): visitor.visit_object_type(self.name, self.info, self.ifcond, - self.base, self.local_members, self.variants) + self.base, self.local_members, self.variants, + self.features) visitor.visit_object_type_flat(self.name, self.info, self.ifcond, - self.members, self.variants) + self.members, self.variants, + self.features) class QAPISchemaMember(object): + """ Represents object members, enum members and features """ role = 'member' def __init__(self, name, ifcond=None):@@ -1418,6 +1452,8 @@ class QAPISchemaMember(object): def describe(self): return "'%s' %s" % (self.name, self._pretty_owner()) +class QAPISchemaFeature(QAPISchemaMember): + role = 'feature' class QAPISchemaObjectTypeMember(QAPISchemaMember): def __init__(self, name, typ, optional, ifcond=None):@@ -1675,7 +1711,7 @@ class QAPISchema(object): ('null', 'null', 'QNull' + pointer_suffix)]: self._def_builtin_type(*t) self.the_empty_object_type = QAPISchemaObjectType( - 'q_empty', None, None, None, None, [], None) + 'q_empty', None, None, None, None, [], None, []) self._def_entity(self.the_empty_object_type) qtypes = ['none', 'qnull', 'qnum', 'qstring', 'qdict', 'qlist',@@ -1685,6 +1721,9 @@ class QAPISchema(object): self._def_entity(QAPISchemaEnumType('QType', None, None, None, qtype_values, 'QTYPE')) + def _make_features(self, features): + return [QAPISchemaFeature(f['name'], f.get('if')) for f in features] + def _make_enum_members(self, values): return [QAPISchemaMember(v['name'], v.get('if')) for v in values]@@ -1721,7 +1760,7 @@ class QAPISchema(object): assert ifcond == typ._ifcond # pylint: disable=protected-access else: self._def_entity(QAPISchemaObjectType(name, info, doc, ifcond, - None, members, None)) + None, members, None, [])) return name def _def_enum_type(self, expr, info, doc):
scripts/qapi/common.py class QAPISchema(object): · line 1798
Quoted 9 lines
scripts/qapi/common.py +3
-1@@ -1752,9 +1791,11 @@ class QAPISchema(object): base = expr.get('base') data = expr['data'] ifcond = expr.get('if') + features = expr.get('features', []) self._def_entity(QAPISchemaObjectType(name, info, doc, ifcond, base, self._make_members(data, info), - None)) + None, + self._make_features(features)))
I like this much better than v1, because it's so close to the IR for similar things.
Quoted 34 lines
scripts/qapi/common.py +1
-1def _make_variant(self, case, typ, ifcond): return QAPISchemaObjectTypeVariant(case, typ, ifcond)@@ -1795,7 +1836,7 @@ class QAPISchema(object): QAPISchemaObjectType(name, info, doc, ifcond, base, members, QAPISchemaObjectTypeVariants(tag_name, tag_member, - variants))) + variants), [])) def _def_alternate_type(self, expr, info, doc): name = expr['alternate']scripts/qapi/doc.py +2
-1@@ -220,7 +220,8 @@ class QAPISchemaGenDocVisitor(qapi.common.QAPISchemaVisitor): body=texi_entity(doc, 'Values', ifcond, member_func=texi_enum_value))) - def visit_object_type(self, name, info, ifcond, base, members, variants): + def visit_object_type(self, name, info, ifcond, base, members, variants, + features): doc = self.cur_doc if base and base.is_implicit(): base = Nonescripts/qapi/introspect.py +0
-0@@ -188,11 +188,15 @@ const QLitObject %(c_name)s = %(c_string)s; self._gen_qlit('[' + element + ']', 'array', {'element-type': element}, ifcond)
scripts/qapi/introspect.py const QLitObject %(c_name)s = %(c_string)s; · line 199
Quoted 10 lines
scripts/qapi/introspect.py +5
-1- def visit_object_type_flat(self, name, info, ifcond, members, variants): + def visit_object_type_flat(self, name, info, ifcond, members, variants, + features): obj = {'members': [self._gen_member(m) for m in members]} if variants: obj.update(self._gen_variants(variants.tag_member.name, variants.variants)) + if features: + obj['features'] = [ (f.name, {'if': f.ifcond}) for f in features ] +
Another place where features have become like enums (see visit_enum_type()). De-duplication seems not worthwhile here.
Quoted 44 lines
scripts/qapi/introspect.py +0
-0self._gen_qlit(name, 'object', obj, ifcond) def visit_alternate_type(self, name, info, ifcond, variants):scripts/qapi/types.py +2
-1@@ -227,7 +227,8 @@ class QAPISchemaGenTypeVisitor(QAPISchemaModularCVisitor): self._genh.add(gen_array(name, element_type)) self._gen_type_cleanup(name) - def visit_object_type(self, name, info, ifcond, base, members, variants): + def visit_object_type(self, name, info, ifcond, base, members, variants, + features): # Nothing to do for the special empty builtin if name == 'q_empty': returnscripts/qapi/visit.py +2
-1@@ -326,7 +326,8 @@ class QAPISchemaGenVisitVisitor(QAPISchemaModularCVisitor): self._genh.add(gen_visit_decl(name)) self._genc.add(gen_visit_list(name, element_type)) - def visit_object_type(self, name, info, ifcond, base, members, variants): + def visit_object_type(self, name, info, ifcond, base, members, variants, + features): # Nothing to do for the special empty builtin if name == 'q_empty': returntests/qapi-schema/double-type.err +1
-1@@ -1,2 +1,2 @@ tests/qapi-schema/double-type.json:2: Unknown key 'command' in struct 'bar' -Valid keys are 'base', 'data', 'if', 'struct'. +Valid keys are 'base', 'data', 'features', 'if', 'struct'.tests/qapi-schema/test-qapi.py +0
-0
tests/qapi-schema/test-qapi.py class QAPISchemaTestVisitor(QAPISchemaVisitor): · line 45
Quoted 9 lines
tests/qapi-schema/test-qapi.py +2
-1@@ -38,7 +38,8 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor): print('array %s %s' % (name, element_type.name)) self._print_if(ifcond) - def visit_object_type(self, name, info, ifcond, base, members, variants): + def visit_object_type(self, name, info, ifcond, base, members, variants, + features): print('object %s' % name) if base: print(' base %s' % base.name)
Printing @features is left to PATCH 2. Okay.
tests/qapi-schema/unknown-expr-key.err line 2
Quoted 8 lines
tests/qapi-schema/unknown-expr-key.err +1
-1@@ -1,2 +1,2 @@ tests/qapi-schema/unknown-expr-key.json:2: Unknown keys 'bogus', 'phony' in struct 'bar' -Valid keys are 'base', 'data', 'if', 'struct'. +Valid keys are 'base', 'data', 'features', 'if', 'struct'.
Looks pretty good now!
Re: [Qemu-devel] [PATCH v2 2/6] tests/qapi-schema: Test for good feature lists in structs
In reply to Kevin WolfQuoted 35 lines · Kevin Wolf
Signed-off-by Kevin Wolf <[email protected]>--- tests/qapi-schema/qapi-schema-test.json | 30 +++++++++++++++++++++++ tests/qapi-schema/qapi-schema-test.out | 32 +++++++++++++++++++++++++ tests/qapi-schema/test-qapi.py | 4 ++++ 3 files changed, 66 insertions(+)tests/qapi-schema/qapi-schema-test.json +20
-0@@ -242,3 +242,33 @@ { 'foo': 'TestIfStruct', 'bar': { 'type': ['TestIfEnum'], 'if': 'defined(TEST_IF_EVT_BAR)' } }, 'if': 'defined(TEST_IF_EVT) && defined(TEST_IF_STRUCT)' } + +# test 'features' for structs + +{ 'struct': 'FeatureStruct0', + 'data': { 'foo': 'int' }, + 'features': [] } +{ 'struct': 'FeatureStruct1', + 'data': { 'foo': 'int' }, + 'features': [ 'feature1' ] } +{ 'struct': 'FeatureStruct2', + 'data': { 'foo': 'int' }, + 'features': [ { 'name': 'feature1' } ] } +{ 'struct': 'FeatureStruct3', + 'data': { 'foo': 'int' }, + 'features': [ 'feature1', 'feature2' ] } +{ 'struct': 'FeatureStruct4', + 'data': { 'namespace-test': 'int' }, + 'features': [ 'namespace-test', 'int', 'name', 'if' ] } + +{ 'struct': 'CondFeatureStruct1',
tests/qapi-schema/qapi-schema-test.json line 274
Quoted 10 lines
tests/qapi-schema/qapi-schema-test.json +10
-0+ 'data': { 'foo': 'int' }, + 'features': [ { 'name': 'feature1', 'if': 'defined(TEST_IF_FEATURE_1)'} ] } +{ 'struct': 'CondFeatureStruct2', + 'data': { 'foo': 'int' }, + 'features': [ { 'name': 'feature1', 'if': 'defined(TEST_IF_FEATURE_1)'}, + { 'name': 'feature2', 'if': 'defined(TEST_IF_FEATURE_2)'} ] } +{ 'struct': 'CondFeatureStruct3', + 'data': { 'foo': 'int' }, + 'features': [ { 'name': 'feature1', 'if': [ 'defined(TEST_IF_COND_1)', + 'defined(TEST_IF_COND_2)'] } ] }
Let's add
{ 'command': 'test-features',
'data': { 'fs0': 'FeatureStruct0',
'fs1': 'FeatureStruct1',
'fs2': 'FeatureStruct2',
'fs3': 'FeatureStruct3',
'cfs1': 'CondFeatureStruct1',
'cfs2': 'CondFeatureStruct2',
'cfs3': 'CondFeatureStruct3' } }because without it, the feature test cases won't generate introspection code.
[...]
Re: [Qemu-devel] [PATCH v2 1/6] qapi: Support features for structs
In reply to Markus ArmbrusterQuoted 8 lines · Markus Armbruster
"Support features for structs" accidentally fits the "Do stuff" commit message anti-pattern. Suggest:
qapi: Add feature flags to struct types
qapi/introspect.json line 178
Quoted 2 lines
qapi/introspect.json +2
-0+# @features: names of features that are supported by this version and build and +# aren't othervise visible through schema introspection (e.g. change
s/othervise/otherwise/
Quoted 1 line
Quoted 1 line
+# of behaviour related to an object type that didn't come with a
Do we have a strong US or UK preference on spelling in user-visible docs? (US would use "behavior")
Quoted 6 lines
qapi/introspect.json line 181
Quoted 2 lines
qapi/introspect.json +2
-0+# syntactic change in the schema of the object type) (since: 4.1) +#Specify "in no particular order", like we do elsewhere.
Let's wrap lines a little earlier.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [PATCH v2 4/6] qapi: Disentangle QAPIDoc code
In reply to Kevin WolfQuoted 7 lines · Kevin Wolf
Documentation comment follow a certain structure: First, we have a text with a general description (called QAPIDoc.body). After this, descriptions of the arguments follow. Finally, we have part that contains various named sections.
The code doesn't show this structure but just checks the right side conditions so it happens to do the right set of things in the right
What are "side conditions"?
Quoted 5 lines
phase. This is hard to follow, and adding support for documentation of features would be even harder.
This restructures the code so that the three parts are clearly separated. The code becomes a bit longer, but easier to follow.
Recommend to mention that output remains unchanged.
Quoted 11 lines
Signed-off-by Kevin Wolf <[email protected]>--- scripts/qapi/common.py | 107 ++++++++++++++++++++++++++++++++--------- 1 file changed, 83 insertions(+), 24 deletions(-)scripts/qapi/common.py +0
-0
scripts/qapi/common.py class QAPIDoc(object): · line 125
Quoted 6 lines
scripts/qapi/common.py +3
-0@@ -120,6 +120,27 @@ class QAPIDoc(object): def connect(self, member): self.member = member + class SymbolPart: + """ + Describes which part of the documentation we're parsing right now.
So SymbolPart is a part of the documentation. Shouldn't it be named DocPart then?
scripts/qapi/common.py class QAPIDoc(object): · line 127
Quoted 2 lines
scripts/qapi/common.py +2
-0+ + BODY means that we're ready to process freeform text into self.body. A
s/freeform/free-form/
scripts/qapi/common.py class QAPIDoc(object): · line 128
Quoted 1 line
scripts/qapi/common.py +1
-0+ symbol name is only allowed if no other text was parsed yet. It is
Start your sentences with a capital letter.
scripts/qapi/common.py class QAPIDoc(object): · line 137
Quoted 9 lines
scripts/qapi/common.py +9
-0+ interpreted as the symbol name that describes the currently documented + object. On getting the second symbol name, we proceed to ARGS. + + ARGS means that we're parsing the arguments section. Any symbol name is + interpreted as an argument and an ArgSection is created for it. + + VARIOUS is the final part where freeform sections may appear. This + includes named sections such as "Return:" as well as unnamed + paragraphs. No symbols are allowed any more in this part.
s/any more/anymore/
Suggest "Symbols are not allowed anymore".
Only expression documentation blocks have these parts. Free-form documentation blocks don't. Peeking ahead at the parent class's code:
we make the complete free-form doc block a BODY part, which is okay.
scripts/qapi/common.py class QAPIDoc(object): · line 138
Quoted 1 line
scripts/qapi/common.py +1
-0+ """
Considering the dearth of doc strings in this code, criticizing yours makes me feel bad, but here goes anyway. This class is not about parsing. Pretty much all of the doc string really belongs to the parent class, or maybe to its .append(). It may well also belong to qapi-code-gen.txt section "Expression documentation".
Speaking of that section: it could use some love, to put it charitably. Mind, I'm not asking you to give it your love ;)
Let me try to write a doc string that actually belongs here:
"""
A documentation part.
Expression documentation blocks consist of
* a BODY part: first line naming the expression, plus an optional overview
* an ARGS part: description of each argument (for commands and events) or member (for structs, unions and alternates),
* a VARIOUS part: optional tagged sections.
Free-form documentation blocks consist only of a BODY part. """
Could throw in a pointer to qapi-code-gen.txt.
This loses (useful!) documentation on how we interpret "symbol names". Should move to the code that actually deals with them.
scripts/qapi/common.py class QAPIDoc(object): · line 139
Quoted 1 line
scripts/qapi/common.py +1
-0+ # Can't make it a subclass of Enum because of Python 2
Thanks for documenting Python 2 contortions! Let's phrase it as a TODO comment.
scripts/qapi/common.py class QAPIDoc(object): · line 140
Quoted 1 line
scripts/qapi/common.py +1
-0+ BODY = 0
Any particular reason for 0?
As far as I can tell, Python enum values commonly start with 1, to make them all true.
scripts/qapi/common.py class QAPIDoc(object): · line 159
Quoted 11 lines
scripts/qapi/common.py +4
-0+ ARGS = 1 + VARIOUS = 2 + def __init__(self, parser, info): # self._parser is used to report errors with QAPIParseError. The # resulting error position depends on the state of the parser.@@ -135,6 +156,7 @@ class QAPIDoc(object): self.sections = [] # the current section self._section = self.body + self._part = QAPIDoc.SymbolPart.BODY
The right hand side is tiresome, but I don't have better ideas.
scripts/qapi/common.py class QAPIDoc(object):
Quoted 4 lines
scripts/qapi/common.py +0
-0def has_section(self, name): """Return True if we have a section with this name."""@@ -154,37 +176,84 @@ class QAPIDoc(object):
def append(self, line):
"""Parse a comment line and add it to the documentation.""" line = line[1:]
if not line:
self._append_freeform(line)
return
if line[0] != ' ':
scripts/qapi/common.py class QAPIDoc(object): · line 186
Quoted 11 lines
scripts/qapi/common.py +8
-0raise QAPIParseError(self._parser, "Missing space after #") line = line[1:] + if self._part == QAPIDoc.SymbolPart.BODY: + self._append_body_line(line) + elif self._part == QAPIDoc.SymbolPart.ARGS: + self._append_args_line(line) + elif self._part == QAPIDoc.SymbolPart.VARIOUS: + self._append_various_line(line) + else: + assert False
Hmm. As far as I can tell, this what we use ._part for. All other occurences assign to it.
If you replace
self._part = QAPIDoc.SymbolPart.BODY
by
self._append_line = self._append_body_line
and so forth, then the whole conditional shrinks to
self._append_line(line)
and we don't have to muck around with enums.
scripts/qapi/common.py class QAPIDoc(object): · line 188
Quoted 2 lines
scripts/qapi/common.py +2
-0+ +
pycodestyle-3 gripes:
scripts/qapi/common.py:196:5: E303 too many blank lines (2)
scripts/qapi/common.py class QAPIDoc(object): · line 198
Quoted 10 lines
scripts/qapi/common.py +10
-0+ def end_comment(self): + self._end_section() + + def _check_named_section(self, line, name): + if name in ('Returns:', 'Since:', + # those are often singular or plural + 'Note:', 'Notes:', + 'Example:', 'Examples:', + 'TODO:'): + self._part = QAPIDoc.SymbolPart.VARIOUS
Hiding such a side effect in a _check_FOO() function isn't so nice, but considering the code you're cleaning up, you got a fairly generous "not so nice" allowance to spend. No need to do anything.
scripts/qapi/common.py class QAPIDoc(object): · line 200
Quoted 2 lines
scripts/qapi/common.py +2
-0+ return True + return False
@line is unused.
Quoted 36 lines
scripts/qapi/common.py +16
-8+ + def _append_body_line(self, line): + name = line.split(' ', 1)[0] # FIXME not nice: things like '# @foo:' and '# @foo: ' aren't # recognized, and get silently treated as ordinary text - if self.symbol: - self._append_symbol_line(line) - elif not self.body.text and line.startswith('@'): + if not self.symbol and not self.body.text and line.startswith('@'): if not line.endswith(':'): raise QAPIParseError(self._parser, "Line should end with :") self.symbol = line[1:-1] # FIXME invalid names other than the empty string aren't flagged if not self.symbol: raise QAPIParseError(self._parser, "Invalid name") + elif self.symbol: + # We already know that we document some symbol + if name.startswith('@') and name.endswith(':'): + self._part = QAPIDoc.SymbolPart.ARGS + self._append_args_line(line) + elif self.symbol and self._check_named_section(line, name): + self._append_various_line(line) + else: + self._append_freeform(line.strip()) else: - self._append_freeform(line) - - def end_comment(self): - self._end_section() + # This is free-form documentation without a symbol + self._append_freeform(line.strip()) - def _append_symbol_line(self, line): + def _append_args_line(self, line): name = line.split(' ', 1)[0]
scripts/qapi/common.py class QAPIDoc(object): · line 233
Quoted 10 lines
scripts/qapi/common.py +2
-5if name.startswith('@') and name.endswith(':'): line = line[len(name)+1:] self._start_args_section(name[1:-1]) - elif name in ('Returns:', 'Since:', - # those are often singular or plural - 'Note:', 'Notes:', - 'Example:', 'Examples:', - 'TODO:'): + elif self._check_named_section(line, name): + return self._append_various_line(line)
Here you return something, and...
scripts/qapi/common.py class QAPIDoc(object): · line 238
Quoted 5 lines
scripts/qapi/common.py +5
-0+ elif (self._section.text.endswith('\n\n') + and line and not line[0].isspace()): + self._start_section() + self._part = QAPIDoc.SymbolPart.VARIOUS + return self._append_various_line(line)
... also here, but ...
scripts/qapi/common.py class QAPIDoc(object): · line 240
Quoted 2 lines
scripts/qapi/common.py +2
-0+ + self._append_freeform(line.strip())
... not here.
Quoted 4 lines
scripts/qapi/common.py +4
-0+ + def _append_various_line(self, line): + name = line.split(' ', 1)[0] +
scripts/qapi/common.py class QAPIDoc(object): · line 254
Quoted 10 lines
scripts/qapi/common.py +7
-0+ if name.startswith('@') and name.endswith(':'): + raise QAPIParseError(self._parser, + "'%s' can't follow '%s' section" + % (name, self.sections[0].name)) + elif self._check_named_section(line, name): line = line[len(name)+1:] self._start_section(name[:-1]) + if (not self._section.name or + not self._section.name.startswith('Example')):
pycodestyle-3 gripes:
scripts/qapi/common.py:283:13: E129 visually indented line with same indent as next logical line
Fix like this:
if (not self._section.name
or not self._section.name.startswith('Example')):
Quoted 21 lines
scripts/qapi/common.py +3
-4+ line = line.strip() + self._append_freeform(line) def _start_args_section(self, name):@@ -194,10 +263,7 @@ class QAPIDoc(object): if name in self.args: raise QAPIParseError(self._parser, "'%s' parameter name duplicated" % name) - if self.sections: - raise QAPIParseError(self._parser, - "'@%s:' can't follow '%s' section" - % (name, self.sections[0].name)) + assert not self.sections self._end_section() self._section = QAPIDoc.ArgSection(name) self.args[name] = self._section@@ -219,13 +285,6 @@ class QAPIDoc(object): self._section = None def _append_freeform(self, line):
scripts/qapi/common.py class QAPIDoc(object): · line 290
Quoted 10 lines
scripts/qapi/common.py +0
-7- in_arg = isinstance(self._section, QAPIDoc.ArgSection) - if (in_arg and self._section.text.endswith('\n\n') - and line and not line[0].isspace()): - self._start_section() - if (in_arg or not self._section.name - or not self._section.name.startswith('Example')): - line = line.strip() match = re.match(r'(@\S+:)', line) if match: raise QAPIParseError(self._parser,
The patch is hard to read (not your fault). I mostly reviewed the result instead.
Quoted 3 lines · Kevin Wolf
This series adds optional feature lists to struct definitions in the QAPI schema and makes use of them to advertise the new behaviour of auto-read-only=on in file-posix.
Found nothing major. Looking forward to v3!
Re: [Qemu-devel] [PATCH v2 5/6] qapi: Allow documentation for features
In reply to Kevin WolfQuoted 60 lines · Kevin Wolf
Features will be documented in a new part introduced by a "Features:" line, after arguments and before named sections.
Signed-off-by Kevin Wolf <[email protected]>--- scripts/qapi/common.py | 43 ++++++++++++++++++++++++++++++++++++++---- scripts/qapi/doc.py | 11 +++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-)scripts/qapi/common.py +10
-1@@ -132,6 +132,9 @@ class QAPIDoc(object): ARGS means that we're parsing the arguments section. Any symbol name is interpreted as an argument and an ArgSection is created for it. + FEATURES means that we're parsing features sections. Any symbol name is + interpreted as a feature. + VARIOUS is the final part where freeform sections may appear. This includes named sections such as "Return:" as well as unnamed paragraphs. No symbols are allowed any more in this part.@@ -139,7 +142,8 @@ class QAPIDoc(object): # Can't make it a subclass of Enum because of Python 2 BODY = 0 ARGS = 1 - VARIOUS = 2 + FEATURES = 2 + VARIOUS = 3 def __init__(self, parser, info): # self._parser is used to report errors with QAPIParseError. The@@ -152,6 +156,7 @@ class QAPIDoc(object): self.body = QAPIDoc.Section() # dict mapping parameter name to ArgSection self.args = OrderedDict() + self.features = OrderedDict() # a list of Section self.sections = [] # the current section@@ -180,6 +185,8 @@ class QAPIDoc(object): self._append_body_line(line) elif self._part == QAPIDoc.SymbolPart.ARGS: self._append_args_line(line) + elif self._part == QAPIDoc.SymbolPart.FEATURES: + self._append_features_line(line) elif self._part == QAPIDoc.SymbolPart.VARIOUS: self._append_various_line(line) else:@@ -215,6 +222,8 @@ class QAPIDoc(object): if name.startswith('@') and name.endswith(':'): self._part = QAPIDoc.SymbolPart.ARGS self._append_args_line(line) + elif line == 'Features:': + self._part = QAPIDoc.SymbolPart.FEATURES elif self.symbol and self._check_named_section(line, name): self._append_various_line(line) else:
scripts/qapi/common.py class QAPIDoc(object): · line 242
Quoted 3 lines
scripts/qapi/common.py +0
-0@@ -231,6 +240,26 @@ class QAPIDoc(object): self._start_args_section(name[1:-1]) elif self._check_named_section(line, name): return self._append_various_line(line)
Here you return something...
scripts/qapi/common.py class QAPIDoc(object): · line 247
Quoted 5 lines
scripts/qapi/common.py +5
-0+ elif (self._section.text.endswith('\n\n') + and line and not line[0].isspace()): + if line == 'Features:': + self._part = QAPIDoc.SymbolPart.FEATURES + return
... and here you don't.
scripts/qapi/common.py class QAPIDoc(object): · line 248
Quoted 1 line
scripts/qapi/common.py +1
-0+ else:
Unnecessary else after return. Let's scratch the else.
Quoted 71 lines
scripts/qapi/common.py +23
-3+ self._start_section() + self._part = QAPIDoc.SymbolPart.VARIOUS + return self._append_various_line(line) + + self._append_freeform(line.strip()) + + def _append_features_line(self, line): + name = line.split(' ', 1)[0] + + if name.startswith('@') and name.endswith(':'): + line = line[len(name)+1:] + self._start_features_section(name[1:-1]) + elif self._check_named_section(line, name): + return self._append_various_line(line) elif (self._section.text.endswith('\n\n') and line and not line[0].isspace()): self._start_section()@@ -256,17 +285,23 @@ class QAPIDoc(object): self._append_freeform(line) - def _start_args_section(self, name): + def _start_symbol_section(self, symbols_dict, name): # FIXME invalid names other than the empty string aren't flagged if not name: raise QAPIParseError(self._parser, "Invalid parameter name") - if name in self.args: + if name in symbols_dict: raise QAPIParseError(self._parser, "'%s' parameter name duplicated" % name) assert not self.sections self._end_section() self._section = QAPIDoc.ArgSection(name) - self.args[name] = self._section + symbols_dict[name] = self._section + + def _start_args_section(self, name): + self._start_symbol_section(self.args, name) + + def _start_features_section(self, name): + self._start_symbol_section(self.features, name) def _start_section(self, name=None): if name in ('Returns', 'Since') and self.has_section(name):scripts/qapi/doc.py +11
-0@@ -181,6 +181,16 @@ def texi_members(doc, what, base, variants, member_func): return '' return '\n@b{%s:}\n@table @asis\n%s@end table\n' % (what, items) +def texi_features(doc): + """Format the table of features""" + items = '' + for section in doc.features.values(): + desc = texi_format(section.text) + items += '@item @code{%s}\n%s' % (section.name, desc) + if not items: + return '' + return '\n@b{Features:}\n@table @asis\n%s@end table\n' % (items) + def texi_sections(doc, ifcond): """Format additional sections following arguments"""@@ -201,6 +211,7 @@ def texi_entity(doc, what, ifcond, base=None, variants=None, member_func=texi_member): return (texi_body(doc) + texi_members(doc, what, base, variants, member_func) + + texi_features(doc) + texi_sections(doc, ifcond))
Re: [Qemu-devel] [PATCH v2 4/6] qapi: Disentangle QAPIDoc code
In reply to Markus ArmbrusterAm 24.05.2019 um 18:11 hat Markus Armbruster geschrieben:
Quoted 34 lines
Quoted 7 lines · Kevin Wolf
Documentation comment follow a certain structure: First, we have a text with a general description (called QAPIDoc.body). After this, descriptions of the arguments follow. Finally, we have part that contains various named sections.
The code doesn't show this structure but just checks the right side conditions so it happens to do the right set of things in the right
What are "side conditions"?
Quoted 5 lines
phase. This is hard to follow, and adding support for documentation of features would be even harder.
This restructures the code so that the three parts are clearly separated. The code becomes a bit longer, but easier to follow.
Recommend to mention that output remains unchanged.
Quoted 14 lines
Signed-off-by Kevin Wolf <[email protected]>--- scripts/qapi/common.py | 107 ++++++++++++++++++++++++++++++++--------- 1 file changed, 83 insertions(+), 24 deletions(-)scripts/qapi/common.py +0
-0@@ -120,6 +120,27 @@ class QAPIDoc(object): def connect(self, member): self.member = member
Quoted 6 lines
scripts/qapi/common.py class QAPIDoc(object): · line 125
Quoted 3 lines
scripts/qapi/common.py +3
-0+ class SymbolPart: + """ + Describes which part of the documentation we're parsing right now.So SymbolPart is a part of the documentation. Shouldn't it be named DocPart then?
That's a better name. I was stuck in the old code (which was concerned about what a symbol name means at which point) rather than thinking about high-level concepts.
Quoted 4 lines
scripts/qapi/common.py class QAPIDoc(object): · line 127
Quoted 2 lines
scripts/qapi/common.py +2
-0+ + BODY means that we're ready to process freeform text into self.body. As/freeform/free-form/
Both are valid spellings and I generally don't expect correct spellings to be corrected, but arguably "free-form" is more standard. I'll change it. (If we were consistent, the method should have been named _append_free_form rather than _append_freeform originally...)
Quoted 3 lines
scripts/qapi/common.py class QAPIDoc(object): · line 128
Quoted 1 line
scripts/qapi/common.py +1
-0+ symbol name is only allowed if no other text was parsed yet. It isStart your sentences with a capital letter.
I would gladly correct a sentence not starting with a capital letter if I could see any. The quoted sentence starts with a capital "A" in the previous line.
Quoted 11 lines
scripts/qapi/common.py class QAPIDoc(object): · line 137
Quoted 9 lines
scripts/qapi/common.py +9
-0+ interpreted as the symbol name that describes the currently documented + object. On getting the second symbol name, we proceed to ARGS. + + ARGS means that we're parsing the arguments section. Any symbol name is + interpreted as an argument and an ArgSection is created for it. + + VARIOUS is the final part where freeform sections may appear. This + includes named sections such as "Return:" as well as unnamed + paragraphs. No symbols are allowed any more in this part.s/any more/anymore/
Again both are valid, but this time, "any more" is the more standard spelling.
Quoted 11 lines
scripts/qapi/common.py class QAPIDoc(object): · line 139
Quoted 1 line
scripts/qapi/common.py +1
-0+ # Can't make it a subclass of Enum because of Python 2Thanks for documenting Python 2 contortions! Let's phrase it as a TODO comment.
scripts/qapi/common.py class QAPIDoc(object): · line 140
Quoted 1 line
scripts/qapi/common.py +1
-0+ BODY = 0Any particular reason for 0?
As far as I can tell, Python enum values commonly start with 1, to make them all true.
If you use enums in a boolean context, you're doing something wrong anyway. *shrug*
I'll change it, it's consistent with real Enum classes where the values becomes non-integer objects (which therefore evaluate as True in boolean contexts).
Quoted 5 lines
Quoted 5 lines
scripts/qapi/common.py +3
-0+ ARGS = 1 + VARIOUS = 2 + def __init__(self, parser, info): # self._parser is used to report errors with QAPIParseError. The
Quoted 8 lines
scripts/qapi/common.py class QAPIDoc(object): · line 159
Quoted 6 lines
scripts/qapi/common.py +1
-0# resulting error position depends on the state of the parser.@@ -135,6 +156,7 @@ class QAPIDoc(object): self.sections = [] # the current section self._section = self.body + self._part = QAPIDoc.SymbolPart.BODYThe right hand side is tiresome, but I don't have better ideas.
This is just what Python enums look like... I could move the class outside of QAPIDoc to save that part of the prefix, but I'd prefer not to.
Quoted 33 lines
scripts/qapi/common.py class QAPIDoc(object):
Quoted 4 lines
scripts/qapi/common.py +0
-0def has_section(self, name): """Return True if we have a section with this name."""@@ -154,37 +176,84 @@ class QAPIDoc(object):def
append(self, line):
"""Parse a comment line and add it to the documentation.""" line = line[1:]
if not line:
self._append_freeform(line)
returnif line[0] != ' ':
scripts/qapi/common.py class QAPIDoc(object): · line 186
Quoted 11 lines
scripts/qapi/common.py +8
-0raise QAPIParseError(self._parser, "Missing space after #") line = line[1:] + if self._part == QAPIDoc.SymbolPart.BODY: + self._append_body_line(line) + elif self._part == QAPIDoc.SymbolPart.ARGS: + self._append_args_line(line) + elif self._part == QAPIDoc.SymbolPart.VARIOUS: + self._append_various_line(line) + else: + assert FalseHmm. As far as I can tell, this what we use ._part for. All other occurences assign to it.
If you replace
self._part = QAPIDoc.SymbolPart.BODY
by
Quoted 7 lines
self._append_line = self._append_body_line
and so forth, then the whole conditional shrinks to
self._append_line(line)and we don't have to muck around with enums.
I could just have added a boolean that decides whether a symbol is an argument or a feature. That would have been a minimal hack that wouldn't involve any enums.
I intentionally decided not to do that because the whole structure of the parser was horribly confusing to me and I felt that introducing a clear state machine would improve its legibility a lot. I still think that this is what it did.
If you don't like a proper state machine, I can do that bool thing. I don't think throwing in function pointers would be very helpful for readers, so we'd get a major code change for no gain.
Kevin
Re: [Qemu-devel] [PATCH v2 2/6] tests/qapi-schema: Test for good feature lists in structs
In reply to Markus ArmbrusterAm 24.05.2019 um 15:29 hat Markus Armbruster geschrieben:
Quoted 11 lines
Let's add
{ 'command': 'test-features', 'data': { 'fs0': 'FeatureStruct0', 'fs1': 'FeatureStruct1', 'fs2': 'FeatureStruct2', 'fs3': 'FeatureStruct3', 'cfs1': 'CondFeatureStruct1', 'cfs2': 'CondFeatureStruct2', 'cfs3': 'CondFeatureStruct3' } }
Quoted 2 lines
because without it, the feature test cases won't generate introspection code.
Of course, like everything else you requested, I'll just do this to get the series off my table, but I'm still curious: Where would introspection code ever be generated for the test cases? I saw neither test code that generates the source files nor reference output that it would be compared against.
Kevin
Re: [Qemu-devel] [PATCH v2 2/6] tests/qapi-schema: Test for good feature lists in structs
Quoted 12 lines · Kevin Wolf
Am 24.05.2019 um 15:29 hat Markus Armbruster geschrieben:
Quoted 11 lines
Let's add
{ 'command': 'test-features', 'data': { 'fs0': 'FeatureStruct0', 'fs1': 'FeatureStruct1', 'fs2': 'FeatureStruct2', 'fs3': 'FeatureStruct3', 'cfs1': 'CondFeatureStruct1', 'cfs2': 'CondFeatureStruct2', 'cfs3': 'CondFeatureStruct3' } }
Quoted 8 lines
Quoted 2 lines
because without it, the feature test cases won't generate introspection code.
Of course, like everything else you requested, I'll just do this to get the series off my table, but I'm still curious: Where would introspection code ever be generated for the test cases? I saw neither test code that generates the source files nor reference output that it would be compared against.
Asking me to explain why I want something done when you can't see it yourself is much, much better than blindly implementing it.
Makefile.include feeds the two positive tests qapi-schema-test.json and doc-good.json to qapi-gen.py.
The .o for the former's .c get linked into a bunch of tests via Make variable $(test-qapi-obj-y). One of them is test-qobject-input-visitor. Its test case "/visitor/input/qapi-introspect" checks the generated QObject conforms to the schema.
qapi-schema.json gets tested end-to-end instead: qmp-cmd-tests tests query-qmp-schema.
Both tests only check schema conformance, they don't compare to expected output. Perhaps they should. But I can still diff the generated qmp-introspect.c manually, which I routinely do when messing with the generator.
Makes sense?
Re: [Qemu-devel] [PATCH v2 2/6] tests/qapi-schema: Test for good feature lists in structs
Am 03.06.2019 um 08:35 hat Markus Armbruster geschrieben:
Quoted 17 lines
Quoted 15 lines · Kevin Wolf
Am 24.05.2019 um 15:29 hat Markus Armbruster geschrieben:
Quoted 13 lines
Let's add
{ 'command': 'test-features', 'data': { 'fs0': 'FeatureStruct0', 'fs1': 'FeatureStruct1', 'fs2': 'FeatureStruct2', 'fs3': 'FeatureStruct3', 'cfs1': 'CondFeatureStruct1', 'cfs2': 'CondFeatureStruct2', 'cfs3': 'CondFeatureStruct3' } }because without it, the feature test cases won't generate introspection code.
Quoted 8 lines
Quoted 5 lines
Of course, like everything else you requested, I'll just do this to get the series off my table, but I'm still curious: Where would introspection code ever be generated for the test cases? I saw neither test code that generates the source files nor reference output that it would be compared against.
Asking me to explain why I want something done when you can't see it yourself is much, much better than blindly implementing it.
Well, adding a command can't hurt anyway, so doing both in parallel seemed like a good option. :-)
Quoted 11 lines
Makefile.include feeds the two positive tests qapi-schema-test.json and doc-good.json to
qapi-gen.py.The .o for the former's .c get linked into a bunch of tests via Make variable $(test-qapi-obj-y). One of them is test-qobject-input-visitor. Its test case "/visitor/input/qapi-introspect" checks the generated QObject conforms to the schema.
qapi-schema.json gets tested end-to-end instead: qmp-cmd-tests tests query-qmp-schema.
Quoted 6 lines
Both tests only check schema conformance, they don't compare to expected output. Perhaps they should. But I can still diff the generated
qmp-introspect.cmanually, which I routinely do when messing with the generator.Makes sense?
Ah, so basically we don't fully check the correctness of the introspection output, but just that two different generated files are consistent with each other. Yes, makes sense.
I just didn't realise that the generated code is actually compiled and linked to some other test cases. My expectation was the schema testing was only in tests/qapi-schema/.
Kevin
Re: [Qemu-devel] [PATCH v2 4/6] qapi: Disentangle QAPIDoc code
In reply to Kevin WolfQuoted 46 lines · Kevin Wolf
Am 24.05.2019 um 18:11 hat Markus Armbruster geschrieben:
Quoted 34 lines
Quoted 7 lines · Kevin Wolf
Documentation comment follow a certain structure: First, we have a text with a general description (called QAPIDoc.body). After this, descriptions of the arguments follow. Finally, we have part that contains various named sections.
The code doesn't show this structure but just checks the right side conditions so it happens to do the right set of things in the right
What are "side conditions"?
Quoted 5 lines
phase. This is hard to follow, and adding support for documentation of features would be even harder.
This restructures the code so that the three parts are clearly separated. The code becomes a bit longer, but easier to follow.
Recommend to mention that output remains unchanged.
Quoted 14 lines
Signed-off-by Kevin Wolf <[email protected]>--- scripts/qapi/common.py | 107 ++++++++++++++++++++++++++++++++--------- 1 file changed, 83 insertions(+), 24 deletions(-)scripts/qapi/common.py +0
-0@@ -120,6 +120,27 @@ class QAPIDoc(object): def connect(self, member): self.member = memberQuoted 6 lines
scripts/qapi/common.py class QAPIDoc(object): · line 125
Quoted 3 lines
scripts/qapi/common.py +3
-0+ class SymbolPart: + """ + Describes which part of the documentation we're parsing right now.So SymbolPart is a part of the documentation. Shouldn't it be named DocPart then?
That's a better name. I was stuck in the old code (which was concerned about what a symbol name means at which point) rather than thinking about high-level concepts.
Quoted 8 lines
Quoted 4 lines
scripts/qapi/common.py class QAPIDoc(object): · line 127
Quoted 2 lines
scripts/qapi/common.py +2
-0+ + BODY means that we're ready to process freeform text into self.body. As/freeform/free-form/
Both are valid spellings and I generally don't expect correct spellings to be corrected, but arguably "free-form" is more standard. I'll change it.
The error message and qapi-code-gen.txt consistently spell it free-form. I prefer consistent spelling, not least for greppability.
Quoted 2 lines
(If we were consistent, the method should have been named
_append_free_formrather than_append_freeformoriginally...)
Yes.
Quoted 7 lines
Quoted 3 lines
scripts/qapi/common.py class QAPIDoc(object): · line 128
Quoted 1 line
scripts/qapi/common.py +1
-0+ symbol name is only allowed if no other text was parsed yet. It isStart your sentences with a capital letter.
I would gladly correct a sentence not starting with a capital letter if I could see any. The quoted sentence starts with a capital "A" in the previous line.
My mistake, I overlooked the "A" at the end of the line.
Quoted 12 lines
Quoted 11 lines
scripts/qapi/common.py class QAPIDoc(object): · line 137
Quoted 9 lines
scripts/qapi/common.py +9
-0+ interpreted as the symbol name that describes the currently documented + object. On getting the second symbol name, we proceed to ARGS. + + ARGS means that we're parsing the arguments section. Any symbol name is + interpreted as an argument and an ArgSection is created for it. + + VARIOUS is the final part where freeform sections may appear. This + includes named sections such as "Return:" as well as unnamed + paragraphs. No symbols are allowed any more in this part.s/any more/anymore/
Quoted 2 lines
Again both are valid, but this time, "any more" is the more standard spelling.
Eric, what's your take?
Quoted 7 lines
Quoted 7 lines
scripts/qapi/common.py class QAPIDoc(object): · line 139
Quoted 1 line
scripts/qapi/common.py +1
-0+ # Can't make it a subclass of Enum because of Python 2Thanks for documenting Python 2 contortions! Let's phrase it as a TODO comment.
Quoted 1 line
scripts/qapi/common.py +1
-0+ BODY = 0
Quoted 7 lines
Quoted 4 lines
Any particular reason for 0?
As far as I can tell, Python enum values commonly start with 1, to make them all true.
If you use enums in a boolean context, you're doing something wrong anyway. *shrug*
No argument. But...
Quoted 3 lines
I'll change it, it's consistent with real Enum classes where the values becomes non-integer objects (which therefore evaluate as True in boolean contexts).
... consistency with real Enum costs us nothing, so let's do it.
Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
scripts/qapi/common.py +3
-0+ ARGS = 1 + VARIOUS = 2 + def __init__(self, parser, info): # self._parser is used to report errors with QAPIParseError. TheQuoted 8 lines
scripts/qapi/common.py class QAPIDoc(object): · line 159
Quoted 6 lines
scripts/qapi/common.py +1
-0# resulting error position depends on the state of the parser.@@ -135,6 +156,7 @@ class QAPIDoc(object): self.sections = [] # the current section self._section = self.body + self._part = QAPIDoc.SymbolPart.BODYThe right hand side is tiresome, but I don't have better ideas.
Quoted 3 lines
This is just what Python enums look like... I could move the class outside of QAPIDoc to save that part of the prefix, but I'd prefer not to.
It's okay.
Quoted 39 lines
Quoted 39 lines
scripts/qapi/common.py class QAPIDoc(object):
Quoted 4 lines
scripts/qapi/common.py +0
-0def has_section(self, name): """Return True if we have a section with this name."""@@ -154,37 +176,84 @@ class QAPIDoc(object):def
append(self, line):
"""Parse a comment line and add it to the documentation.""" line = line[1:]
if not line:
self._append_freeform(line)
returnif line[0] != ' ':
scripts/qapi/common.py class QAPIDoc(object): · line 186
Quoted 11 lines
scripts/qapi/common.py +8
-0raise QAPIParseError(self._parser, "Missing space after #") line = line[1:] + if self._part == QAPIDoc.SymbolPart.BODY: + self._append_body_line(line) + elif self._part == QAPIDoc.SymbolPart.ARGS: + self._append_args_line(line) + elif self._part == QAPIDoc.SymbolPart.VARIOUS: + self._append_various_line(line) + else: + assert FalseHmm. As far as I can tell, this what we use ._part for. All other occurences assign to it.
If you replace
self._part = QAPIDoc.SymbolPart.BODY
by
self._append_line = self._append_body_line
and so forth, then the whole conditional shrinks to
self._append_line(line)
Quoted 8 lines
Quoted 1 line
and we don't have to muck around with enums.
I could just have added a boolean that decides whether a symbol is an argument or a feature. That would have been a minimal hack that wouldn't involve any enums.
I intentionally decided not to do that because the whole structure of the parser was horribly confusing to me
Not just to you.
Quoted 7 lines
and I felt that introducing a
clear state machine would improve its legibility a lot. I still think that this is what it did.If you don't like a proper state machine, I can do that bool thing. I don't think throwing in function pointers would be very helpful for readers, so we'd get a major code change for no gain.
There's more than one way to code a state machine. Encoding the state as enum / switch over next state is one. Encoding the state as pointer / jump to next state is another.