qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH v1 5/6] qapi: generate a qapi_stringify_TYPENAME met


From: Daniel P. Berrange
Subject: [Qemu-devel] [PATCH v1 5/6] qapi: generate a qapi_stringify_TYPENAME method for all types
Date: Tue, 7 Jun 2016 11:11:14 +0100

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 <address@hidden>
---
 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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]