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