xref: /openbmc/qemu/tests/qapi-schema/test-qapi.py (revision 563bd35d87f7bcab644381319d6247e8aee8a4f4)
1#
2# QAPI parser test harness
3#
4# Copyright (c) 2013 Red Hat Inc.
5#
6# Authors:
7#  Markus Armbruster <armbru@redhat.com>
8#
9# This work is licensed under the terms of the GNU GPL, version 2 or later.
10# See the COPYING file in the top-level directory.
11#
12
13from __future__ import print_function
14import sys
15from qapi.common import QAPIError, QAPISchema, QAPISchemaVisitor
16
17
18class QAPISchemaTestVisitor(QAPISchemaVisitor):
19
20    def visit_module(self, name):
21        print('module %s' % name)
22
23    def visit_include(self, name, info):
24        print('include %s' % name)
25
26    def visit_enum_type(self, name, info, ifcond, members, prefix):
27        print('enum %s' % name)
28        if prefix:
29            print('    prefix %s' % prefix)
30        for m in members:
31            print('    member %s' % m.name)
32        self._print_if(ifcond)
33
34    def visit_object_type(self, name, info, ifcond, base, members, variants):
35        print('object %s' % name)
36        if base:
37            print('    base %s' % base.name)
38        for m in members:
39            print('    member %s: %s optional=%s'
40                  % (m.name, m.type.name, m.optional))
41        self._print_variants(variants)
42        self._print_if(ifcond)
43
44    def visit_alternate_type(self, name, info, ifcond, variants):
45        print('alternate %s' % name)
46        self._print_variants(variants)
47        self._print_if(ifcond)
48
49    def visit_command(self, name, info, ifcond, arg_type, ret_type, gen,
50                      success_response, boxed, allow_oob, allow_preconfig):
51        print('command %s %s -> %s'
52              % (name, arg_type and arg_type.name,
53                 ret_type and ret_type.name))
54        print('   gen=%s success_response=%s boxed=%s oob=%s preconfig=%s'
55              % (gen, success_response, boxed, allow_oob, allow_preconfig))
56        self._print_if(ifcond)
57
58    def visit_event(self, name, info, ifcond, arg_type, boxed):
59        print('event %s %s' % (name, arg_type and arg_type.name))
60        print('   boxed=%s' % boxed)
61        self._print_if(ifcond)
62
63    @staticmethod
64    def _print_variants(variants):
65        if variants:
66            print('    tag %s' % variants.tag_member.name)
67            for v in variants.variants:
68                print('    case %s: %s' % (v.name, v.type.name))
69
70    @staticmethod
71    def _print_if(ifcond, indent=4):
72        if ifcond:
73            print('%sif %s' % (' ' * indent, ifcond))
74
75
76try:
77    schema = QAPISchema(sys.argv[1])
78except QAPIError as err:
79    print(err, file=sys.stderr)
80    exit(1)
81
82schema.visit(QAPISchemaTestVisitor())
83
84for doc in schema.docs:
85    if doc.symbol:
86        print('doc symbol=%s' % doc.symbol)
87    else:
88        print('doc freeform')
89    print('    body=\n%s' % doc.body.text)
90    for arg, section in doc.args.items():
91        print('    arg=%s\n%s' % (arg, section.text))
92    for section in doc.sections:
93        print('    section=%s\n%s' % (section.name, section.text))
94