xref: /openbmc/qemu/scripts/qapi/source.py (revision 06d4c71f)
1#
2# QAPI frontend source file info
3#
4# Copyright (c) 2019 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.
10# See the COPYING file in the top-level directory.
11
12import copy
13import sys
14
15
16class QAPISchemaPragma:
17    def __init__(self):
18        # Are documentation comments required?
19        self.doc_required = False
20        # Whitelist of commands allowed to return a non-dictionary
21        self.returns_whitelist = []
22        # Whitelist of entities allowed to violate case conventions
23        self.name_case_whitelist = []
24
25
26class QAPISourceInfo:
27    def __init__(self, fname, line, parent):
28        self.fname = fname
29        self.line = line
30        self.parent = parent
31        self.pragma = parent.pragma if parent else QAPISchemaPragma()
32        self.defn_meta = None
33        self.defn_name = None
34
35    def set_defn(self, meta, name):
36        self.defn_meta = meta
37        self.defn_name = name
38
39    def next_line(self):
40        info = copy.copy(self)
41        info.line += 1
42        return info
43
44    def loc(self):
45        if self.fname is None:
46            return sys.argv[0]
47        ret = self.fname
48        if self.line is not None:
49            ret += ':%d' % self.line
50        return ret
51
52    def in_defn(self):
53        if self.defn_name:
54            return "%s: In %s '%s':\n" % (self.fname,
55                                          self.defn_meta, self.defn_name)
56        return ''
57
58    def include_path(self):
59        ret = ''
60        parent = self.parent
61        while parent:
62            ret = 'In file included from %s:\n' % parent.loc() + ret
63            parent = parent.parent
64        return ret
65
66    def __str__(self):
67        return self.include_path() + self.in_defn() + self.loc()
68