xref: /openbmc/qemu/tests/qapi-schema/test-qapi.py (revision 6cc32b0e14b3f91e15a9511d90b222abc1df391d)
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(m.ifcond, indent=8)
33        self._print_if(ifcond)
34
35    def visit_object_type(self, name, info, ifcond, base, members, variants):
36        print('object %s' % name)
37        if base:
38            print('    base %s' % base.name)
39        for m in members:
40            print('    member %s: %s optional=%s'
41                  % (m.name, m.type.name, m.optional))
42        self._print_variants(variants)
43        self._print_if(ifcond)
44
45    def visit_alternate_type(self, name, info, ifcond, variants):
46        print('alternate %s' % name)
47        self._print_variants(variants)
48        self._print_if(ifcond)
49
50    def visit_command(self, name, info, ifcond, arg_type, ret_type, gen,
51                      success_response, boxed, allow_oob, allow_preconfig):
52        print('command %s %s -> %s'
53              % (name, arg_type and arg_type.name,
54                 ret_type and ret_type.name))
55        print('   gen=%s success_response=%s boxed=%s oob=%s preconfig=%s'
56              % (gen, success_response, boxed, allow_oob, allow_preconfig))
57        self._print_if(ifcond)
58
59    def visit_event(self, name, info, ifcond, arg_type, boxed):
60        print('event %s %s' % (name, arg_type and arg_type.name))
61        print('   boxed=%s' % boxed)
62        self._print_if(ifcond)
63
64    @staticmethod
65    def _print_variants(variants):
66        if variants:
67            print('    tag %s' % variants.tag_member.name)
68            for v in variants.variants:
69                print('    case %s: %s' % (v.name, v.type.name))
70
71    @staticmethod
72    def _print_if(ifcond, indent=4):
73        if ifcond:
74            print('%sif %s' % (' ' * indent, ifcond))
75
76
77try:
78    schema = QAPISchema(sys.argv[1])
79except QAPIError as err:
80    print(err, file=sys.stderr)
81    exit(1)
82
83schema.visit(QAPISchemaTestVisitor())
84
85for doc in schema.docs:
86    if doc.symbol:
87        print('doc symbol=%s' % doc.symbol)
88    else:
89        print('doc freeform')
90    print('    body=\n%s' % doc.body.text)
91    for arg, section in doc.args.items():
92        print('    arg=%s\n%s' % (arg, section.text))
93    for section in doc.sections:
94        print('    section=%s\n%s' % (section.name, section.text))
95