From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36376) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bADzD-0008Fe-Lv for qemu-devel@nongnu.org; Tue, 07 Jun 2016 06:11:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bADz7-00055z-6w for qemu-devel@nongnu.org; Tue, 07 Jun 2016 06:11:50 -0400 From: "Daniel P. Berrange" Date: Tue, 7 Jun 2016 11:11:14 +0100 Message-Id: <1465294275-8733-6-git-send-email-berrange@redhat.com> In-Reply-To: <1465294275-8733-1-git-send-email-berrange@redhat.com> References: <1465294275-8733-1-git-send-email-berrange@redhat.com> Subject: [Qemu-devel] [PATCH v1 5/6] qapi: generate a qapi_stringify_TYPENAME method for all types List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, Kevin Wolf , Max Reitz , Markus Armbruster , Michael Roth , Eric Blake , "Daniel P. Berrange" There are sometimes cases where one might wish to have a pretty string representation of a QAPI type. For example, the 'qemu-img info' tool wants to print out ImageInfoSpecific type in a humand friendly format. Also when debugging problems in code it is often useful to insert code to print out a QAPI object. To address this, add a qapi_stringify_TYPENAME() method for all types which wraps around the TextOutputVisitor to turn objects into pretty strings. Signed-off-by: Daniel P. Berrange --- scripts/qapi-types.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py index 437cf6c..c3eef41 100644 --- a/scripts/qapi-types.py +++ b/scripts/qapi-types.py @@ -167,6 +167,40 @@ void qapi_free_%(c_name)s(%(c_name)s *obj) return ret +def gen_type_stringify_decl(name): + ret = mcgen(''' + +char *qapi_stringify_%(c_name)s(%(c_name)s *obj, int extraIndent, int skipLevel); +''', + c_name=c_name(name)) + return ret + + +def gen_type_stringify(name): + ret = mcgen(''' + +char *qapi_stringify_%(c_name)s(%(c_name)s *obj, int extraIndent, int skipLevel) +{ + TextOutputVisitor *tov; + Visitor *v; + char *ret; + + if (!obj) { + return NULL; + } + + tov = text_output_visitor_new(extraIndent, skipLevel); + v = text_output_get_visitor(tov); + visit_type_%(c_name)s(v, NULL, &obj, NULL); + ret = text_output_get_string(tov); + text_output_visitor_cleanup(tov); + return ret; +} +''', + c_name=c_name(name)) + return ret + + class QAPISchemaGenTypeVisitor(QAPISchemaVisitor): def __init__(self): self.decl = None @@ -197,6 +231,10 @@ class QAPISchemaGenTypeVisitor(QAPISchemaVisitor): self.decl += gen_type_cleanup_decl(name) self.defn += gen_type_cleanup(name) + def _gen_type_stringify(self, name): + self.decl += gen_type_stringify_decl(name) + self.defn += gen_type_stringify(name) + def visit_enum_type(self, name, info, values, prefix): # Special case for our lone builtin enum type # TODO use something cleaner than existence of info @@ -215,10 +253,14 @@ class QAPISchemaGenTypeVisitor(QAPISchemaVisitor): self._btin += gen_type_cleanup_decl(name) if do_builtins: self.defn += gen_type_cleanup(name) + self._btin += gen_type_stringify_decl(name) + if do_builtins: + self.defn += gen_type_stringify(name) else: self._fwdecl += gen_fwd_object_or_array(name) self.decl += gen_array(name, element_type) self._gen_type_cleanup(name) + self._gen_type_stringify(name) def visit_object_type(self, name, info, base, members, variants): # Nothing to do for the special empty builtin @@ -233,11 +275,13 @@ class QAPISchemaGenTypeVisitor(QAPISchemaVisitor): if not name.startswith('q_'): # implicit types won't be directly allocated/freed self._gen_type_cleanup(name) + self._gen_type_stringify(name) def visit_alternate_type(self, name, info, variants): self._fwdecl += gen_fwd_object_or_array(name) self.decl += gen_object(name, None, [variants.tag_member], variants) self._gen_type_cleanup(name) + self._gen_type_stringify(name) # If you link code generated from multiple schemata, you want only one # instance of the code for built-in types. Generate it only when @@ -289,6 +333,7 @@ h_comment = ''' fdef.write(mcgen(''' #include "qemu/osdep.h" #include "qapi/dealloc-visitor.h" +#include "qapi/text-output-visitor.h" #include "%(prefix)sqapi-types.h" #include "%(prefix)sqapi-visit.h" ''', -- 2.5.5