Skip to content

Commit 2ebb09f

Browse files
berrangeMarkus Armbruster
authored andcommitted
qapi: expose all schema features to code
This replaces use of the constants from the QapiSpecialFeatures enum, with constants from the auto-generate QapiFeatures enum in qapi-features.h The 'deprecated' and 'unstable' features still have a little bit of special handling, being force defined to be the 1st + 2nd features in the enum, regardless of whether they're used in the schema. This retains compatibility with common code that references the features via the QapiSpecialFeatures constants. Signed-off-by: Daniel P. Berrangé <[email protected]> Message-ID: <[email protected]> Reviewed-by: Markus Armbruster <[email protected]> [Imports tidied up with isort] Signed-off-by: Markus Armbruster <[email protected]>
1 parent ba27dcc commit 2ebb09f

File tree

13 files changed

+110
-7
lines changed

13 files changed

+110
-7
lines changed

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3444,6 +3444,7 @@ qapi_gen_depends = [ meson.current_source_dir() / 'scripts/qapi/__init__.py',
34443444
meson.current_source_dir() / 'scripts/qapi/schema.py',
34453445
meson.current_source_dir() / 'scripts/qapi/source.py',
34463446
meson.current_source_dir() / 'scripts/qapi/types.py',
3447+
meson.current_source_dir() / 'scripts/qapi/features.py',
34473448
meson.current_source_dir() / 'scripts/qapi/visit.py',
34483449
meson.current_source_dir() / 'scripts/qapi-gen.py'
34493450
]

scripts/qapi/commands.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ def visit_begin(self, schema: QAPISchema) -> None:
355355
#include "qemu/osdep.h"
356356
#include "%(prefix)sqapi-commands.h"
357357
#include "%(prefix)sqapi-init-commands.h"
358+
#include "%(prefix)sqapi-features.h"
358359
359360
void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds)
360361
{

scripts/qapi/features.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
QAPI features generator
3+
4+
Copyright 2024 Red Hat
5+
6+
This work is licensed under the terms of the GNU GPL, version 2.
7+
# See the COPYING file in the top-level directory.
8+
"""
9+
10+
from typing import ValuesView
11+
12+
from .common import c_enum_const, c_name
13+
from .gen import QAPISchemaMonolithicCVisitor
14+
from .schema import QAPISchema, QAPISchemaFeature
15+
16+
17+
class QAPISchemaGenFeatureVisitor(QAPISchemaMonolithicCVisitor):
18+
19+
def __init__(self, prefix: str):
20+
super().__init__(
21+
prefix, 'qapi-features',
22+
' * Schema-defined QAPI features',
23+
__doc__)
24+
25+
self.features: ValuesView[QAPISchemaFeature]
26+
27+
def visit_begin(self, schema: QAPISchema) -> None:
28+
self.features = schema.features()
29+
self._genh.add("#include \"qapi/util.h\"\n\n")
30+
31+
def visit_end(self) -> None:
32+
self._genh.add("typedef enum {\n")
33+
for f in self.features:
34+
self._genh.add(f" {c_enum_const('qapi_feature', f.name)}")
35+
if f.name in QAPISchemaFeature.SPECIAL_NAMES:
36+
self._genh.add(f" = {c_enum_const('qapi', f.name)},\n")
37+
else:
38+
self._genh.add(",\n")
39+
40+
self._genh.add("} " + c_name('QapiFeature') + ";\n")
41+
42+
43+
def gen_features(schema: QAPISchema,
44+
output_dir: str,
45+
prefix: str) -> None:
46+
vis = QAPISchemaGenFeatureVisitor(prefix)
47+
schema.visit(vis)
48+
vis.write(output_dir)

scripts/qapi/gen.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@
4242

4343

4444
def gen_features(features: Sequence[QAPISchemaFeature]) -> str:
45-
featenum = [f"1u << {c_enum_const('qapi', feat.name)}"
46-
for feat in features if feat.is_special()]
47-
return ' | '.join(featenum) or '0'
45+
feats = [f"1u << {c_enum_const('qapi_feature', feat.name)}"
46+
for feat in features]
47+
return ' | '.join(feats) or '0'
4848

4949

5050
class QAPIGen:

scripts/qapi/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from .common import must_match
1616
from .error import QAPIError
1717
from .events import gen_events
18+
from .features import gen_features
1819
from .introspect import gen_introspect
1920
from .schema import QAPISchema
2021
from .types import gen_types
@@ -49,6 +50,7 @@ def generate(schema_file: str,
4950

5051
schema = QAPISchema(schema_file)
5152
gen_types(schema, output_dir, prefix, builtins)
53+
gen_features(schema, output_dir, prefix)
5254
gen_visit(schema, output_dir, prefix, builtins)
5355
gen_commands(schema, output_dir, prefix, gen_tracing)
5456
gen_events(schema, output_dir, prefix)

scripts/qapi/schema.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
List,
3030
Optional,
3131
Union,
32+
ValuesView,
3233
cast,
3334
)
3435

@@ -933,8 +934,11 @@ def connect_doc(self, doc: Optional[QAPIDoc]) -> None:
933934
class QAPISchemaFeature(QAPISchemaMember):
934935
role = 'feature'
935936

937+
# Features which are standardized across all schemas
938+
SPECIAL_NAMES = ['deprecated', 'unstable']
939+
936940
def is_special(self) -> bool:
937-
return self.name in ('deprecated', 'unstable')
941+
return self.name in QAPISchemaFeature.SPECIAL_NAMES
938942

939943

940944
class QAPISchemaObjectTypeMember(QAPISchemaMember):
@@ -1138,6 +1142,16 @@ def __init__(self, fname: str):
11381142
self._entity_list: List[QAPISchemaEntity] = []
11391143
self._entity_dict: Dict[str, QAPISchemaDefinition] = {}
11401144
self._module_dict: Dict[str, QAPISchemaModule] = OrderedDict()
1145+
# NB, values in the dict will identify the first encountered
1146+
# usage of a named feature only
1147+
self._feature_dict: Dict[str, QAPISchemaFeature] = OrderedDict()
1148+
1149+
# All schemas get the names defined in the QapiSpecialFeature enum.
1150+
# Rely on dict iteration order matching insertion order so that
1151+
# the special names are emitted first when generating code.
1152+
for f in QAPISchemaFeature.SPECIAL_NAMES:
1153+
self._feature_dict[f] = QAPISchemaFeature(f, None)
1154+
11411155
self._schema_dir = os.path.dirname(fname)
11421156
self._make_module(QAPISchemaModule.BUILTIN_MODULE_NAME)
11431157
self._make_module(fname)
@@ -1147,6 +1161,9 @@ def __init__(self, fname: str):
11471161
self._def_exprs(exprs)
11481162
self.check()
11491163

1164+
def features(self) -> ValuesView[QAPISchemaFeature]:
1165+
return self._feature_dict.values()
1166+
11501167
def _def_entity(self, ent: QAPISchemaEntity) -> None:
11511168
self._entity_list.append(ent)
11521169

@@ -1258,6 +1275,12 @@ def _make_features(
12581275
) -> List[QAPISchemaFeature]:
12591276
if features is None:
12601277
return []
1278+
1279+
for f in features:
1280+
feat = QAPISchemaFeature(f['name'], info)
1281+
if feat.name not in self._feature_dict:
1282+
self._feature_dict[feat.name] = feat
1283+
12611284
return [QAPISchemaFeature(f['name'], info,
12621285
QAPISchemaIfCond(f.get('if')))
12631286
for f in features]
@@ -1485,6 +1508,12 @@ def check(self) -> None:
14851508
for doc in self.docs:
14861509
doc.check()
14871510

1511+
features = list(self._feature_dict.values())
1512+
if len(features) > 64:
1513+
raise QAPISemError(
1514+
features[64].info,
1515+
"Maximum of 64 schema features is permitted")
1516+
14881517
def visit(self, visitor: QAPISchemaVisitor) -> None:
14891518
visitor.visit_begin(self)
14901519
for mod in self._module_dict.values():

scripts/qapi/types.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,14 @@ def _begin_user_module(self, name: str) -> None:
304304
#include "qapi/dealloc-visitor.h"
305305
#include "%(types)s.h"
306306
#include "%(visit)s.h"
307+
#include "%(prefix)sqapi-features.h"
307308
''',
308-
types=types, visit=visit))
309+
types=types, visit=visit,
310+
prefix=self._prefix))
309311
self._genh.preamble_add(mcgen('''
310312
#include "qapi/qapi-builtin-types.h"
311-
'''))
313+
''',
314+
prefix=self._prefix))
312315

313316
def visit_begin(self, schema: QAPISchema) -> None:
314317
# gen_object() is recursive, ensure it doesn't visit the empty type

scripts/qapi/visit.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,9 @@ def _begin_user_module(self, name: str) -> None:
356356
#include "qemu/osdep.h"
357357
#include "qapi/error.h"
358358
#include "%(visit)s.h"
359+
#include "%(prefix)sqapi-features.h"
359360
''',
360-
visit=visit))
361+
visit=visit, prefix=self._prefix))
361362
self._genh.preamble_add(mcgen('''
362363
#include "qapi/qapi-builtin-visit.h"
363364
#include "%(types)s.h"

tests/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ test_qapi_outputs = [
1616
'test-qapi-events-sub-sub-module.h',
1717
'test-qapi-events.c',
1818
'test-qapi-events.h',
19+
'test-qapi-features.c',
20+
'test-qapi-features.h',
1921
'test-qapi-init-commands.c',
2022
'test-qapi-init-commands.h',
2123
'test-qapi-introspect.c',
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
features-too-many.json: In command 'go-fish':
2+
features-too-many.json:2: Maximum of 64 schema features is permitted

0 commit comments

Comments
 (0)