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